<!DOCTYPE html>












  


<html class="theme-next mist use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2"/>
<meta name="theme-color" content="#222">



  
  
  <link rel="stylesheet" href="/lib/needsharebutton/needsharebutton.css">




  
  
    
    
  <script src="/lib/pace/pace.min.js?v=1.0.2"></script>
  <link href="/lib/pace/pace-theme-minimal.min.css?v=1.0.2" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=6.3.0" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/avatar.ico?v=6.3.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/avatar.ico?v=6.3.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/avatar.ico?v=6.3.0">


  <link rel="mask-icon" href="/avatar.ico?v=6.3.0" color="#222">









<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '6.3.0',
    sidebar: {"position":"left","display":"always","offset":12,"b2t":false,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="本处难度分档以个人实力为参照系难度1：半小时内想出，半小时内ac难度2：半小时想不出，看题解，服气难度3：半小时想不出，看题解，ac后依然觉得难度很大">
<meta name="keywords" content="算法竞赛进阶指南">
<meta property="og:type" content="article">
<meta property="og:title" content="【训练】算法竞赛进阶指南-3数学30题">
<meta property="og:url" content="http://zory.cf/posts/82f7.html">
<meta property="og:site_name" content="Zory的个人博客">
<meta property="og:description" content="本处难度分档以个人实力为参照系难度1：半小时内想出，半小时内ac难度2：半小时想不出，看题解，服气难度3：半小时想不出，看题解，ac后依然觉得难度很大">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://zory.cf/images/算法竞赛进阶指南/3数学.png">
<meta property="og:image" content="http://zory.cf/images/算法竞赛进阶指南/Counting_Swaps.jpg">
<meta property="og:updated_time" content="2018-08-29T08:16:02.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="【训练】算法竞赛进阶指南-3数学30题">
<meta name="twitter:description" content="本处难度分档以个人实力为参照系难度1：半小时内想出，半小时内ac难度2：半小时想不出，看题解，服气难度3：半小时想不出，看题解，ac后依然觉得难度很大">
<meta name="twitter:image" content="http://zory.cf/images/算法竞赛进阶指南/3数学.png">






  <link rel="canonical" href="http://zory.cf/posts/82f7.html"/>



<script type="text/javascript" id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>【训练】算法竞赛进阶指南-3数学30题 | Zory的个人博客</title>
  






  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?6c38ba8648d29103b8d122f998787d71";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style type="text/css">
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion {
      .logo-line-before i { left: initial; }
      .logo-line-after i { right: initial; }
    }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Zory的个人博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <h1 class="site-subtitle" itemprop="description">人生即使不能走最短路，仍是连通图</h1>
      
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">
    <a href="/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页Home</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">
    <a href="/archives/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />历程Archives<span class="badge">354</span></a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">
    <a href="/categories/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-th"></i> <br />分类Categories<span class="badge">4</span></a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">
    <a href="/tags/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />标签Tags<span class="badge">133</span></a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-template">
    <a href="/categories/OI之路" rel="section">
      <i class="menu-item-icon fa fa-fw fa-telegram"></i> <br />OI之路</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-problems">
    <a href="/categories/刷题" rel="section">
      <i class="menu-item-icon fa fa-fw fa-list-alt"></i> <br />刷题Problems</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-friends">
    <a href="/friends/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-user-circle"></i> <br />友链Friends</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-english">
    <a href="/English/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-etsy"></i> <br />English知识体系</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-boutique">
    <a href="/tags/原创精华/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-handshake-o"></i> <br />原创精华</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-top">
    <a href="/top/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-signal"></i> <br />排行榜Hot</a>
  </li>

      
      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />找文章强烈建议使用-搜索Search</a>
        </li>
      
    </ul>
  

  
    

  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="这个搜索超级好用哦！找文章必备！" spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://zory.cf/posts/82f7.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Zory">
      <meta itemprop="description" content="人生即使不能走最短路，仍是连通图">
      <meta itemprop="image" content="http://ouk2t1pgs.bkt.clouddn.com/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Zory的个人博客">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">【训练】算法竞赛进阶指南-3数学30题
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建于：2018-07-19 00:00:00" itemprop="dateCreated datePublished" datetime="2018-07-19T00:00:00+08:00">2018-07-19</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改于：2018-08-29 16:16:02" itemprop="dateModified" datetime="2018-08-29T16:16:02+08:00">2018-08-29</time>
              
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/刷题/" itemprop="url" rel="index"><span itemprop="name">刷题</span></a></span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/posts/82f7.html#comments" itemprop="discussionUrl">
                  <span class="post-meta-item-text">评论：</span> <span class="post-comments-count valine-comment-count" data-xid="/posts/82f7.html" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/posts/82f7.html" class="leancloud_visitors" data-flag-title="【训练】算法竞赛进阶指南-3数学30题">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">热度：</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>本处难度分档以个人实力为参照系<br>难度1：半小时内想出，半小时内ac<br>难度2：半小时想不出，看题解，服气<br>难度3：半小时想不出，看题解，ac后依然觉得难度很大</p>
<p><img src="../images/算法竞赛进阶指南/3数学.png" alt=""></p>
<a id="more"></a>
<h2 id="0x30-数学"><a href="#0x30-数学" class="headerlink" title="0x30 数学"></a>0x30 数学</h2><p><a href="http://contest-hunter.org:83/contest/0x3B%E3%80%8C%E6%95%B0%E5%AD%A6%E7%9F%A5%E8%AF%86%E3%80%8D%E7%BB%83%E4%B9%A0" target="_blank" rel="noopener">题目</a></p>
<h3 id="1-poj2689-Prime-Distance"><a href="#1-poj2689-Prime-Distance" class="headerlink" title="1 poj2689 Prime Distance"></a>1 poj2689 Prime Distance</h3><p>7.19 难度2<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>有一个很妙的做法：采用类似埃筛的方法筛素数<br>所以只要线性预处理出前面$\sqrt R$内的素数p，然后在后面搞一搞就好了</p>
<p>复杂度计算：<br>$$<br>O(<br>\sum_{质数p \leq \sqrt R} \frac{R-L}{p}<br>)<br>$$<br>然后根据什么调和级数之类的东西，$1+1/2+1/3+1/4….1/n ≈ log_2 n$<br>所以说复杂度就大概是$O((R-L) \times log_2 \sqrt R)$</p>
<p>最后提醒各位特判一下1<br>并且数组的下标是相对位置，错了好多次<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXN=<span class="number">310</span>;</div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> INF=<span class="number">0x3f3f3f3f</span>;</div><div class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</div><div class="line"><span class="keyword">typedef</span> <span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">long</span> ull;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">mymax</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span> </span>&#123;<span class="keyword">return</span> x&gt;y?x:y;&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">mymin</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span> </span>&#123;<span class="keyword">return</span> x&lt;y?x:y;&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">myabs</span><span class="params">(<span class="keyword">int</span> x)</span> </span>&#123;<span class="keyword">return</span> x&gt;<span class="number">0</span>?x:-x;&#125;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXNUM=<span class="number">100000</span>;</div><div class="line"><span class="keyword">int</span> pr=<span class="number">0</span>,prime[MAXNUM];</div><div class="line"><span class="keyword">bool</span> v[MAXNUM];</div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">pre</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">2</span>;i&lt;MAXNUM;i++)</div><div class="line">    &#123;</div><div class="line">        <span class="keyword">if</span>(!v[i]) prime[++pr]=i;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j&lt;=pr and i*prime[j]&lt;MAXNUM;j++)</div><div class="line">        &#123;</div><div class="line">            v[i*prime[j]]=<span class="number">1</span>;</div><div class="line">            <span class="keyword">if</span>(i%prime[j]==<span class="number">0</span>) <span class="keyword">break</span>;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">bool</span> bk[<span class="number">1100000</span>];</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    pre();</div><div class="line"></div><div class="line">    <span class="keyword">int</span> l,r;</div><div class="line">    <span class="keyword">while</span>(<span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&amp;l,&amp;r)!=EOF)</div><div class="line">    &#123;</div><div class="line">        <span class="built_in">memset</span>(bk,<span class="number">1</span>,<span class="keyword">sizeof</span> bk);</div><div class="line">        <span class="keyword">if</span>(l==<span class="number">1</span>) bk[<span class="number">0</span>]=<span class="number">0</span>;<span class="comment">//debug</span></div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=pr;i++)</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> j=l/prime[i];j&lt;=r/prime[i];j++)</div><div class="line">                <span class="keyword">if</span>(l&lt;=j*prime[i] and j&gt;<span class="number">1</span>)</div><div class="line">                    bk[j*prime[i]-l]=<span class="number">0</span>;<span class="comment">//debug</span></div><div class="line"></div><div class="line">        <span class="keyword">bool</span> two=<span class="number">0</span>;</div><div class="line">        <span class="keyword">int</span> mix=<span class="number">0</span>,miy=INF,mxx=<span class="number">0</span>,mxy=<span class="number">0</span>,lst=<span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;=r-l;i++)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">if</span>(!bk[i]) <span class="keyword">continue</span>;</div><div class="line">            <span class="keyword">int</span> tmp=l+i;</div><div class="line"></div><div class="line">            <span class="keyword">if</span>(lst&gt;<span class="number">0</span> and tmp-lst&lt;miy-mix) mix=lst,miy=tmp,two=<span class="number">1</span>;</div><div class="line">            <span class="keyword">if</span>(lst&gt;<span class="number">0</span> and tmp-lst&gt;mxy-mxx) mxx=lst,mxy=tmp,two=<span class="number">1</span>;</div><div class="line"></div><div class="line">            lst=tmp;</div><div class="line">        &#125;</div><div class="line"></div><div class="line">        <span class="keyword">if</span>(two) <span class="built_in">printf</span>(<span class="string">"%d,%d are closest, %d,%d are most distant.\n"</span>,mix,miy,mxx,mxy);</div><div class="line">        <span class="keyword">else</span> <span class="built_in">puts</span>(<span class="string">"There are no adjacent primes."</span>);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div></p>
<h3 id="2-3101-阶乘分解"><a href="#2-3101-阶乘分解" class="headerlink" title="2 3101 阶乘分解"></a>2 3101 阶乘分解</h3><p>7.19 难度1<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>枚举素数p，考虑贡献，对于次幂为1，有$\frac{n}{p}$个<br>对于次幂为t，有$\frac{n}{p^t}$个，新贡献恰好为$\frac{n}{p^t}$<br>所以，枚举每个素数，然后以log的时间计算贡献即可<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXN=<span class="number">1100000</span>;</div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> INF=<span class="number">0x3f3f3f3f</span>;</div><div class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</div><div class="line"><span class="keyword">typedef</span> <span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">long</span> ull;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">mymax</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span> </span>&#123;<span class="keyword">return</span> x&gt;y?x:y;&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">mymin</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span> </span>&#123;<span class="keyword">return</span> x&lt;y?x:y;&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">myabs</span><span class="params">(<span class="keyword">int</span> x)</span> </span>&#123;<span class="keyword">return</span> x&gt;<span class="number">0</span>?x:-x;&#125;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXNUM=<span class="number">1000000</span>;</div><div class="line"><span class="keyword">int</span> pr=<span class="number">0</span>,prime[MAXNUM];</div><div class="line"><span class="keyword">bool</span> v[MAXNUM];</div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">pre</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">2</span>;i&lt;MAXNUM;i++)</div><div class="line">    &#123;</div><div class="line">        <span class="keyword">if</span>(!v[i]) prime[++pr]=i;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j&lt;=pr and i*prime[j]&lt;MAXNUM;j++)</div><div class="line">        &#123;</div><div class="line">            v[i*prime[j]]=<span class="number">1</span>;</div><div class="line">            <span class="keyword">if</span>(i%prime[j]==<span class="number">0</span>) <span class="keyword">break</span>;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    pre();</div><div class="line"></div><div class="line">    <span class="keyword">int</span> n;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;n);</div><div class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> k=<span class="number">1</span>;k&lt;=pr;k++)</div><div class="line">    &#123;</div><div class="line">        <span class="keyword">int</span> tot=<span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span>(ll kn=prime[k];kn&lt;=n;kn*=prime[k]) tot+=n/kn;</div><div class="line">        <span class="keyword">if</span>(tot&gt;<span class="number">0</span>) <span class="built_in">printf</span>(<span class="string">"%d %d\n"</span>,prime[k],tot);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div></p>
<h3 id="3-HAOI2007-反素数"><a href="#3-HAOI2007-反素数" class="headerlink" title="3 HAOI2007 反素数"></a>3 HAOI2007 反素数</h3><p>8.6 难度2<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>找找反素数共有的性质吧</p>
<p>性质1：<strong>如果约数个数相同，根据性质，应该取最小的</strong></p>
<p>性质2：<strong>素因子只有2,3,5,7,11,13,17,19,23</strong><br>证明：<br>回忆约数个数公式，只与每个素数的次幂有关。<br>如果使用更大的素因子，前面这9个中至少有一个空缺（否则超出题目范围）<br>如果用这个空缺替换，次幂不变，会得到更小的数字，违背性质1</p>
<p>性质3：若将反素数表示为<br>$<br>2^{k_2} \times<br>3^{k_3} \times<br>5^{k_5} \times<br>7^{k_7} \times<br>11^{k_{11}} \times<br>13^{k_{13}} \times<br>17^{k_{17}} \times<br>19^{k_{19}} \times<br>23^{k_{23}}<br>$<br>则<br>$<br>k_2 \geq<br>k_3 \geq<br>k_5 \geq<br>k_7 \geq<br>k_{11} \geq<br>k_{13} \geq<br>k_{17} \geq<br>k_{19} \geq<br>k_{23} \geq<br>0<br>$<br>证明：<br>如果不递减，把某个逆序对交换后，<br>可以得到相同约数下更小的数字，违背性质1</p>
<p>最后用dfs枚举一下就好了<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXN=<span class="number">110000</span>;</div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> INF=<span class="number">0x3f3f3f3f</span>;</div><div class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</div><div class="line"><span class="keyword">typedef</span> <span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">long</span> ull;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">mymax</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span> </span>&#123;<span class="keyword">return</span> x&gt;y?x:y;&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">mymin</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span> </span>&#123;<span class="keyword">return</span> x&lt;y?x:y;&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">myabs</span><span class="params">(<span class="keyword">int</span> x)</span> </span>&#123;<span class="keyword">return</span> x&gt;<span class="number">0</span>?x:-x;&#125;</div><div class="line"></div><div class="line"><span class="keyword">int</span> mxn;</div><div class="line"><span class="keyword">int</span> mxg=<span class="number">1</span>,ans=<span class="number">1</span>;<span class="comment">//最小</span></div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> p[<span class="number">10</span>]=&#123;<span class="number">0</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">5</span>,<span class="number">7</span>,<span class="number">11</span>,<span class="number">13</span>,<span class="number">17</span>,<span class="number">19</span>,<span class="number">23</span>&#125;;</div><div class="line"></div><div class="line"><span class="keyword">int</span> k[<span class="number">10</span>];</div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">dfs</span><span class="params">(<span class="keyword">int</span> now,<span class="keyword">int</span> nowg,<span class="keyword">int</span> num)</span></div><div class="line"></span>&#123;</div><div class="line">    <span class="keyword">if</span>(num&gt;mxn) <span class="keyword">return</span>;</div><div class="line">    <span class="keyword">if</span>(now==<span class="number">10</span>)</div><div class="line">    &#123;</div><div class="line">        <span class="keyword">if</span>(nowg&gt;mxg) mxg=nowg,ans=num;</div><div class="line">        <span class="keyword">else</span> <span class="keyword">if</span>(nowg==mxg and num&lt;ans) ans=num;<span class="comment">//debug，不能保证数字递增</span></div><div class="line">        <span class="keyword">return</span>;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="keyword">int</span> tmp=<span class="number">1</span>;</div><div class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;=k[now<span class="number">-1</span>] and tmp&lt;=mxn and ll(num)*tmp&lt;=mxn;i++)<span class="comment">//debug num写成now</span></div><div class="line">    &#123;</div><div class="line">        k[now]=i;</div><div class="line">        dfs(now+<span class="number">1</span>,nowg*(i+<span class="number">1</span>),num*tmp);</div><div class="line">        tmp*=p[now];</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;mxn);</div><div class="line">    k[<span class="number">0</span>]=<span class="number">30</span>;</div><div class="line">    dfs(<span class="number">1</span>,<span class="number">1</span>,<span class="number">1</span>);</div><div class="line">    <span class="built_in">printf</span>(<span class="string">"%d"</span>,ans);</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div></p>
<h3 id="4-CQOI2007-余数求和"><a href="#4-CQOI2007-余数求和" class="headerlink" title="4 CQOI2007 余数求和"></a>4 CQOI2007 余数求和</h3><p>8.6 难度1<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>先化简柿子<br>$<br>ans<br>=\sum_{i=1}^n k-\lfloor \frac{k}{i} \rfloor \times i<br>=nk - \sum_{i=1}^k \lfloor \frac{k}{i} \rfloor \times i<br>$<br>这是因为当i&gt;k时，结果就是k</p>
<p>然后这东西初看时线性的，还是会超时<br>如果有经验的话，可以一眼看出用一个技巧解决<br>详见<a href="../2003-01/套路集锦.html">套路集锦</a>中枚举方法3<br>本来不知道怎么算复杂度，反正比k小，曾经听说期望根号<br>现在已经把书上的证明补充上去了，其实也不复杂</p>
<p>总之就这样枚举过去，等差数列推推柿子就好了<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXN=<span class="number">110000</span>;</div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> INF=<span class="number">0x3f3f3f3f</span>;</div><div class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</div><div class="line"><span class="keyword">typedef</span> <span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">long</span> ull;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">mymax</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span> </span>&#123;<span class="keyword">return</span> x&gt;y?x:y;&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">mymin</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span> </span>&#123;<span class="keyword">return</span> x&lt;y?x:y;&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">myabs</span><span class="params">(<span class="keyword">int</span> x)</span> </span>&#123;<span class="keyword">return</span> x&gt;<span class="number">0</span>?x:-x;&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    <span class="keyword">int</span> n,k;<span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&amp;n,&amp;k);</div><div class="line">    ll ans=ll(n)*k;</div><div class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=k and i&lt;=n;)<span class="comment">//debug 也要判n</span></div><div class="line">    &#123;</div><div class="line">        <span class="keyword">int</span> lst=k/(k/i);</div><div class="line">        <span class="keyword">if</span>(lst&gt;n) lst=n;<span class="comment">//debug</span></div><div class="line">        ans-=ll(k/i)*(i+lst)*(lst-i+<span class="number">1</span>)/<span class="number">2</span>;<span class="comment">//debug long long</span></div><div class="line">        i=lst+<span class="number">1</span>;</div><div class="line">    &#125;</div><div class="line">    <span class="built_in">printf</span>(<span class="string">"%lld"</span>,ans);</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div></p>
<h3 id="5-poj3090-Visible-Lattice-Points"><a href="#5-poj3090-Visible-Lattice-Points" class="headerlink" title="5 poj3090 Visible Lattice Points"></a>5 poj3090 Visible Lattice Points</h3><p>8.7 难度2<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>先分析题目，不难发现要求的是gcd(1~n,1~n)=1的数量</p>
<p>那么可以直接用莫比乌斯反演<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXN=<span class="number">110000</span>;</div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> INF=<span class="number">0x3f3f3f3f</span>;</div><div class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</div><div class="line"><span class="keyword">typedef</span> <span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">long</span> ull;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">mymax</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span> </span>&#123;<span class="keyword">return</span> x&gt;y?x:y;&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">mymin</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span> </span>&#123;<span class="keyword">return</span> x&lt;y?x:y;&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">myabs</span><span class="params">(<span class="keyword">int</span> x)</span> </span>&#123;<span class="keyword">return</span> x&gt;<span class="number">0</span>?x:-x;&#125;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXNUM=<span class="number">1001</span>;</div><div class="line"><span class="keyword">int</span> prime[MAXNUM],pr=<span class="number">0</span>;</div><div class="line"><span class="keyword">bool</span> v[MAXNUM];</div><div class="line"><span class="keyword">int</span> mu[MAXNUM];</div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">pre</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    mu[<span class="number">1</span>]=<span class="number">1</span>;</div><div class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">2</span>;i&lt;MAXNUM;i++)</div><div class="line">    &#123;</div><div class="line">        <span class="keyword">if</span>(!v[i]) prime[++pr]=i,mu[i]=<span class="number">-1</span>;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j&lt;=pr and i*prime[j]&lt;MAXNUM;j++)</div><div class="line">        &#123;</div><div class="line">            v[i*prime[j]]=<span class="number">1</span>;</div><div class="line">            <span class="keyword">if</span>(i%prime[j]==<span class="number">0</span>) <span class="keyword">break</span>;<span class="comment">//mu=0</span></div><div class="line">            <span class="keyword">else</span> mu[i*prime[j]]=-mu[i];</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;MAXNUM;i++) mu[i]+=mu[i<span class="number">-1</span>];</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    pre();</div><div class="line"></div><div class="line">    <span class="keyword">int</span> T;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;T);<span class="keyword">int</span> ct=<span class="number">0</span>;</div><div class="line">    <span class="keyword">while</span>(T--)</div><div class="line">    &#123;</div><div class="line">        <span class="keyword">int</span> n;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;n);</div><div class="line"></div><div class="line">        <span class="keyword">int</span> f1=<span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">int</span> lst=n/(n/i);</div><div class="line">            <span class="keyword">int</span> F=(n/i)*(n/i);</div><div class="line">            f1+=(mu[lst]-mu[i<span class="number">-1</span>])*F;</div><div class="line">            i=lst+<span class="number">1</span>;</div><div class="line">        &#125;</div><div class="line">        <span class="built_in">printf</span>(<span class="string">"%d %d %d\n"</span>,++ct,n,<span class="number">2</span>+f1);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>那么我们尝试用欧拉搞一搞<br>其实就是因为长宽相同，那么对于每一列，累加一下欧拉就好了<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXN=<span class="number">110000</span>;</div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> INF=<span class="number">0x3f3f3f3f</span>;</div><div class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</div><div class="line"><span class="keyword">typedef</span> <span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">long</span> ull;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">mymax</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span> </span>&#123;<span class="keyword">return</span> x&gt;y?x:y;&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">mymin</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span> </span>&#123;<span class="keyword">return</span> x&lt;y?x:y;&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">myabs</span><span class="params">(<span class="keyword">int</span> x)</span> </span>&#123;<span class="keyword">return</span> x&gt;<span class="number">0</span>?x:-x;&#125;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXNUM=<span class="number">1001</span>;</div><div class="line"><span class="keyword">int</span> prime[MAXNUM],pr=<span class="number">0</span>;</div><div class="line"><span class="keyword">bool</span> v[MAXNUM];</div><div class="line"><span class="keyword">int</span> phi[MAXNUM];</div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">pre</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    phi[<span class="number">1</span>]=<span class="number">1</span>;</div><div class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">2</span>;i&lt;MAXNUM;i++)</div><div class="line">    &#123;</div><div class="line">        <span class="keyword">if</span>(!v[i]) prime[++pr]=i,phi[i]=i<span class="number">-1</span>;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j&lt;=pr and i*prime[j]&lt;MAXNUM;j++)</div><div class="line">        &#123;</div><div class="line">            v[i*prime[j]]=<span class="number">1</span>;</div><div class="line">            <span class="keyword">if</span>(i%prime[j]==<span class="number">0</span>)</div><div class="line">            &#123;</div><div class="line">                phi[i*prime[j]]=phi[i]*prime[j];</div><div class="line">                <span class="keyword">break</span>;</div><div class="line">            &#125;</div><div class="line">            <span class="keyword">else</span> phi[i*prime[j]]=phi[i]*(prime[j]<span class="number">-1</span>);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    pre();</div><div class="line"></div><div class="line">    <span class="keyword">int</span> T;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;T);<span class="keyword">int</span> ct=<span class="number">0</span>;</div><div class="line">    <span class="keyword">while</span>(T--)</div><div class="line">    &#123;</div><div class="line">        <span class="keyword">int</span> n;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;n);</div><div class="line"></div><div class="line">        <span class="keyword">int</span> ans=<span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">2</span>;i&lt;=n;i++) ans+=phi[i];</div><div class="line">        <span class="built_in">printf</span>(<span class="string">"%d %d %d\n"</span>,++ct,n,<span class="number">3</span>+ans*<span class="number">2</span>);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div></p>
<h3 id="6-poj3696-The-Luckiest-number"><a href="#6-poj3696-The-Luckiest-number" class="headerlink" title="6 poj3696 The Luckiest number"></a>6 poj3696 The Luckiest number</h3><p>8.7 难度3<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>x个8连在一起的数字，可以用$\frac{8}{9} (10^x-1)$表示<br>那么题目要求$L | \frac{8}{9} (10^x-1)$<br>等效于$9L | 8(10^x-1)$<br>设$d=gcd(L,8)$假设条件满足，那么L中的偶因子是8的约数<br>两边同时除以d后，左边不再有偶因子，则右边剩下的偶因子没有意义<br>$\frac{9L}{d} | 10^x-1$<br>转化成$10^x=1 (\mod \frac{9L}{d})$</p>
<p>当$gcd(10,\frac{9L}{d})=1$时，<br>根据中<a href="a06a.html">欧拉函数与欧拉定理的定理8</a>，<br>可枚举其约数，快速幂判断即可<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXN=<span class="number">110000</span>;</div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> INF=<span class="number">0x3f3f3f3f</span>;</div><div class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</div><div class="line"><span class="keyword">typedef</span> <span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">long</span> ull;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">mymax</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span> </span>&#123;<span class="keyword">return</span> x&gt;y?x:y;&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">mymin</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span> </span>&#123;<span class="keyword">return</span> x&lt;y?x:y;&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">myabs</span><span class="params">(<span class="keyword">int</span> x)</span> </span>&#123;<span class="keyword">return</span> x&gt;<span class="number">0</span>?x:-x;&#125;</div><div class="line"></div><div class="line"><span class="function">ll <span class="title">gcd</span><span class="params">(ll x,ll y)</span> </span>&#123;<span class="keyword">return</span> (y==<span class="number">0</span>)?x:gcd(y,x%y);&#125;</div><div class="line"><span class="function">ll <span class="title">qmul</span><span class="params">(ll a,ll e,ll Mod)</span></div><div class="line"></span>&#123;</div><div class="line">    ll ans=<span class="number">0</span>;</div><div class="line">    <span class="keyword">while</span>(e&gt;<span class="number">0</span>)</div><div class="line">    &#123;</div><div class="line">        <span class="keyword">if</span>(e&amp;<span class="number">1</span>) ans=(ans+a)%Mod;</div><div class="line">        e&gt;&gt;=<span class="number">1</span>;a=(a+a)%Mod;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> ans;</div><div class="line">&#125;</div><div class="line"><span class="function">ll <span class="title">qpower</span><span class="params">(ll a,ll e,ll Mod)</span></div><div class="line"></span>&#123;</div><div class="line">    ll ans=<span class="number">1</span>;</div><div class="line">    <span class="keyword">while</span>(e&gt;<span class="number">0</span>)</div><div class="line">    &#123;</div><div class="line">        <span class="keyword">if</span>(e&amp;<span class="number">1</span>) ans=qmul(ans,a,Mod);</div><div class="line">        e&gt;&gt;=<span class="number">1</span>;a=qmul(a,a,Mod);</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> ans;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function">ll <span class="title">getphi</span><span class="params">(ll tmp)</span></div><div class="line"></span>&#123;</div><div class="line">    ll phi=tmp;</div><div class="line">    <span class="keyword">for</span>(ll i=<span class="number">2</span>;i*i&lt;=tmp;i++)</div><div class="line">        <span class="keyword">if</span>(tmp%i==<span class="number">0</span>)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">while</span>(tmp%i==<span class="number">0</span>) tmp/=i;</div><div class="line">            phi=phi/i*(i<span class="number">-1</span>);<span class="comment">//debug phi*(i-1)/i 这里可能爆</span></div><div class="line">        &#125;</div><div class="line">    <span class="keyword">if</span>(tmp&gt;<span class="number">1</span>) phi=phi/tmp*(tmp<span class="number">-1</span>);<span class="comment">//debug</span></div><div class="line">    <span class="keyword">return</span> phi;</div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    <span class="keyword">int</span> ct=<span class="number">0</span>;</div><div class="line">    <span class="keyword">while</span>(<span class="number">1</span>)</div><div class="line">    &#123;</div><div class="line">        <span class="keyword">int</span> L;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;L);</div><div class="line">        <span class="keyword">if</span>(L==<span class="number">0</span>) <span class="keyword">break</span>;</div><div class="line">        <span class="built_in">printf</span>(<span class="string">"Case %d: "</span>,++ct);</div><div class="line"></div><div class="line">        ll tmp=(ll)L*<span class="number">9</span>/gcd(L,<span class="number">8</span>);</div><div class="line"></div><div class="line">        <span class="keyword">if</span>(gcd(<span class="number">10</span>,tmp)==<span class="number">1</span>)</div><div class="line">        &#123;</div><div class="line">            ll phi=getphi(tmp);</div><div class="line"></div><div class="line">            ll ans=phi;</div><div class="line">            <span class="keyword">for</span>(ll i=<span class="number">1</span>;i*i&lt;=phi;i++)</div><div class="line">                <span class="keyword">if</span>(phi%i==<span class="number">0</span>)</div><div class="line">                &#123;</div><div class="line">                    <span class="keyword">if</span>(i&lt;ans and qpower(<span class="number">10</span>,i,tmp)==<span class="number">1</span>) ans=i;</div><div class="line">                    <span class="keyword">if</span>(i*i!=phi and phi/i&lt;ans and qpower(<span class="number">10</span>,phi/i,tmp)==<span class="number">1</span>) ans=phi/i;</div><div class="line">                &#125;</div><div class="line">            <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>,ans);</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">else</span> <span class="built_in">puts</span>(<span class="string">"0"</span>);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div></p>
<h3 id="7-bzoj2973-1801-石头游戏"><a href="#7-bzoj2973-1801-石头游戏" class="headerlink" title="7 bzoj2973 1801 石头游戏"></a>7 bzoj2973 1801 石头游戏</h3><p>8.10 难度1<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>脑残题<br>但是题目居然不说清楚：如果操作违法，要把它丢掉<br>还有就是一些sb错误，耽误了一个早上<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">namespace</span> mine</div><div class="line">&#123;</div><div class="line">    <span class="keyword">const</span> <span class="keyword">int</span> INF=<span class="number">0x3f3f3f3f</span>;</div><div class="line">    <span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</div><div class="line">    <span class="keyword">typedef</span> <span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">long</span> ull;</div><div class="line">    <span class="function"><span class="keyword">int</span> <span class="title">mymax</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span> </span>&#123;<span class="keyword">return</span> x&gt;y?x:y;&#125;</div><div class="line">    <span class="function"><span class="keyword">int</span> <span class="title">mymin</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span> </span>&#123;<span class="keyword">return</span> x&lt;y?x:y;&#125;</div><div class="line">    <span class="function"><span class="keyword">int</span> <span class="title">myabs</span><span class="params">(<span class="keyword">int</span> x)</span> </span>&#123;<span class="keyword">return</span> x&gt;<span class="number">0</span>?x:-x;&#125;</div><div class="line"></div><div class="line">    <span class="keyword">int</span> n,m;</div><div class="line">    <span class="keyword">struct</span> Matrix</div><div class="line">    &#123;</div><div class="line">        ll a[<span class="number">100</span>][<span class="number">100</span>];</div><div class="line">        Matrix()</div><div class="line">        &#123;</div><div class="line">            <span class="built_in">memset</span>(a,<span class="number">0</span>,<span class="keyword">sizeof</span> a);</div><div class="line">        &#125;</div><div class="line">    &#125;;</div><div class="line">    <span class="function">Matrix <span class="title">pre</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        Matrix ans;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=<span class="number">90</span>;i++) ans.a[i][i]=<span class="number">1</span>;</div><div class="line">        <span class="keyword">return</span> ans;</div><div class="line">    &#125;</div><div class="line">    <span class="function">Matrix <span class="title">cheng</span><span class="params">(Matrix a,Matrix b)</span></div><div class="line">    </span>&#123;</div><div class="line">        Matrix c;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=<span class="number">90</span>;i++)</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j&lt;=<span class="number">90</span>;j++)</div><div class="line">                <span class="keyword">for</span>(<span class="keyword">int</span> k=<span class="number">1</span>;k&lt;=<span class="number">90</span>;k++)</div><div class="line">                    c.a[i][j]+=a.a[i][k]*b.a[k][j];</div><div class="line">        <span class="keyword">return</span> c;</div><div class="line">    &#125;</div><div class="line">    <span class="function">Matrix <span class="title">qpower</span><span class="params">(Matrix a,<span class="keyword">int</span> e)</span></div><div class="line">    </span>&#123;</div><div class="line">        Matrix ans=pre();</div><div class="line">        <span class="keyword">while</span>(e&gt;<span class="number">0</span>)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">if</span>(e&amp;<span class="number">1</span>) ans=cheng(ans,a);</div><div class="line">            a=cheng(a,a);e&gt;&gt;=<span class="number">1</span>;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> ans;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">int</span> <span class="title">gcd</span><span class="params">(<span class="keyword">int</span> a,<span class="keyword">int</span> b)</span> </span>&#123;<span class="keyword">return</span> (b==<span class="number">0</span>)?a:gcd(b,a%b);&#125;</div><div class="line">    <span class="function"><span class="keyword">int</span> <span class="title">lcm</span><span class="params">(<span class="keyword">int</span> a,<span class="keyword">int</span> b)</span> </span>&#123;<span class="keyword">return</span> a/gcd(a,b)*b;&#125;</div><div class="line"></div><div class="line">    Matrix op[<span class="number">100</span>];</div><div class="line">    <span class="function"><span class="keyword">int</span> <span class="title">calc</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span> </span>&#123;<span class="keyword">return</span> (x<span class="number">-1</span>)*m+y;&#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">int</span> ti,act;<span class="built_in">scanf</span>(<span class="string">"%d%d%d%d"</span>,&amp;n,&amp;m,&amp;ti,&amp;act);</div><div class="line">        <span class="keyword">char</span> mp[<span class="number">10</span>][<span class="number">10</span>];</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++) <span class="built_in">scanf</span>(<span class="string">"%s"</span>,mp[i]+<span class="number">1</span>);</div><div class="line">        <span class="keyword">int</span> tlen=<span class="number">1</span>;</div><div class="line">        <span class="keyword">char</span> oper[<span class="number">10</span>][<span class="number">10</span>];<span class="keyword">int</span> oplen[<span class="number">10</span>];</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;=act<span class="number">-1</span>;i++)</div><div class="line">        &#123;</div><div class="line">            <span class="built_in">scanf</span>(<span class="string">"%s"</span>,oper[i]+<span class="number">1</span>);</div><div class="line">            oplen[i]=<span class="built_in">strlen</span>(oper[i]+<span class="number">1</span>);</div><div class="line">            tlen=lcm(tlen,oplen[i]);</div><div class="line">        &#125;</div><div class="line"></div><div class="line">        <span class="keyword">int</span> now[<span class="number">10</span>];<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;=act<span class="number">-1</span>;i++) now[i]=<span class="number">1</span>;<span class="comment">//debug 忘记初始化</span></div><div class="line"></div><div class="line">        Matrix all=pre();</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> s=<span class="number">1</span>;s&lt;=tlen;s++)</div><div class="line">        &#123;</div><div class="line">            op[s]=pre();</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++)</div><div class="line">                <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j&lt;=m;j++)</div><div class="line">                &#123;</div><div class="line">                    <span class="keyword">int</span> id=mp[i][j]-<span class="string">'0'</span>,t=now[id];</div><div class="line">                    <span class="keyword">if</span>(<span class="built_in">isdigit</span>(oper[id][t])) op[s].a[calc(i,j)][n*m+<span class="number">1</span>]=oper[id][t]-<span class="string">'0'</span>;</div><div class="line">                    <span class="keyword">else</span> <span class="keyword">if</span>(oper[id][t]==<span class="string">'D'</span>) op[s].a[calc(i,j)][calc(i,j)]=<span class="number">0</span>;</div><div class="line">                    <span class="keyword">else</span><span class="comment">//推</span></div><div class="line">                    &#123;</div><div class="line">                        op[s].a[calc(i,j)][calc(i,j)]=<span class="number">0</span>;</div><div class="line"></div><div class="line">                        <span class="keyword">int</span> nx=i,ny=j;</div><div class="line">                        <span class="keyword">if</span>(oper[id][t]==<span class="string">'N'</span>) nx--;<span class="keyword">if</span>(oper[id][t]==<span class="string">'S'</span>) nx++;</div><div class="line">                        <span class="keyword">if</span>(oper[id][t]==<span class="string">'W'</span>) ny--;<span class="keyword">if</span>(oper[id][t]==<span class="string">'E'</span>) ny++;</div><div class="line"></div><div class="line">                        <span class="keyword">if</span>(<span class="number">1</span>&lt;=nx and nx&lt;=n and <span class="number">1</span>&lt;=ny and ny&lt;=m) op[s].a[calc(nx,ny)][calc(i,j)]=<span class="number">1</span>;</div><div class="line">                    &#125;</div><div class="line">                &#125;</div><div class="line">            all=cheng(op[s],all);</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;=act<span class="number">-1</span>;i++) <span class="keyword">if</span>( (++now[i])==oplen[i]+<span class="number">1</span>) now[i]=<span class="number">1</span>;<span class="comment">//debug 本来写(++now[i])==oplen[i]</span></div><div class="line">        &#125;</div><div class="line"></div><div class="line">        Matrix st;st.a[n*m+<span class="number">1</span>][<span class="number">1</span>]=<span class="number">1</span>;</div><div class="line">        Matrix tmp=pre();<span class="keyword">for</span>(<span class="keyword">int</span> s=ti%tlen;s&gt;=<span class="number">1</span>;s--) tmp=cheng(tmp,op[s]);</div><div class="line">        st=cheng( cheng(tmp,qpower(all,ti/tlen)) ,st);</div><div class="line"></div><div class="line">        ll ans=<span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n*m;i++) <span class="keyword">if</span>(st.a[i][<span class="number">1</span>]&gt;ans) ans=st.a[i][<span class="number">1</span>];</div><div class="line">        <span class="built_in">printf</span>(<span class="string">"%lld"</span>,ans);</div><div class="line">    &#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    mine::main();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div></p>
<h3 id="8-JSOI2008-球形空间产生器"><a href="#8-JSOI2008-球形空间产生器" class="headerlink" title="8 JSOI2008 球形空间产生器"></a>8 JSOI2008 球形空间产生器</h3><p>8.10 难度2<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>先推推柿子<br>如果把n+1个点，与球心的距离表示出来，会得出一个多元二次方程组<br>为了变成一次，考虑把相邻的相减<br>$\sum (a_{i,j}-x_j)^2-(a_{i+1,j}-x_j)^2=0$<br>$\sum 2(a_{i+1,j}-a_{i,j})x_j=\sum a_{i+1,j}^2-a_{i,j}^2$<br>然后高斯消元就好了<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">namespace</span> mine</div><div class="line">&#123;</div><div class="line">    <span class="function"><span class="keyword">double</span> <span class="title">myabs</span><span class="params">(<span class="keyword">double</span> x)</span> </span>&#123;<span class="keyword">return</span> x&gt;<span class="number">0</span>?x:-x;&#125;</div><div class="line">    <span class="keyword">const</span> <span class="keyword">double</span> eps=<span class="number">1e-8</span>;</div><div class="line"></div><div class="line">    <span class="keyword">const</span> <span class="keyword">int</span> MAXN=<span class="number">20</span>;</div><div class="line">    <span class="keyword">double</span> p[MAXN][MAXN];</div><div class="line">    <span class="keyword">double</span> a[MAXN][MAXN],b[MAXN];<span class="comment">//系数、常数</span></div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">gauss</span><span class="params">(<span class="keyword">int</span> n)</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++)<span class="comment">//目标为xi</span></div><div class="line">        &#123;</div><div class="line">            <span class="keyword">int</span> nx=<span class="number">-1</span>;</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> j=i;j&lt;=n;j++) <span class="keyword">if</span>(myabs(a[j][i])&gt;eps) &#123;nx=j;<span class="keyword">break</span>;&#125;</div><div class="line">            <span class="keyword">if</span>(nx&lt;<span class="number">0</span>) <span class="keyword">continue</span>;</div><div class="line">            swap(b[i],b[nx]);<span class="keyword">for</span>(<span class="keyword">int</span> k=<span class="number">1</span>;k&lt;=n;k++) swap(a[i][k],a[nx][k]);</div><div class="line"></div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j&lt;=n;j++)</div><div class="line">            &#123;</div><div class="line">                <span class="keyword">if</span>(i==j) <span class="keyword">continue</span>;</div><div class="line">                <span class="keyword">double</span> t=a[j][i]/a[i][i];<span class="comment">//确保分母不为0</span></div><div class="line">                b[j]-=b[i]*t;<span class="keyword">for</span>(<span class="keyword">int</span> k=i;k&lt;=n;k++) a[j][k]-=a[i][k]*t;</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">int</span> n;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;n);</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n+<span class="number">1</span>;i++)</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j&lt;=n;j++)</div><div class="line">                <span class="built_in">scanf</span>(<span class="string">"%lf"</span>,&amp;p[i][j]);</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++)</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j&lt;=n;j++)</div><div class="line">            &#123;</div><div class="line">                a[i][j]=<span class="number">2</span>*(p[i+<span class="number">1</span>][j]-p[i][j]);</div><div class="line">                b[i]+=p[i+<span class="number">1</span>][j]*p[i+<span class="number">1</span>][j]-p[i][j]*p[i][j];</div><div class="line">            &#125;</div><div class="line">        gauss(n);</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++) <span class="built_in">printf</span>(<span class="string">"%.3lf "</span>,b[i]/a[i][i]);</div><div class="line">    &#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    mine::main();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div></p>
<h3 id="9-poj1830-开关问题"><a href="#9-poj1830-开关问题" class="headerlink" title="9 poj1830 开关问题"></a>9 poj1830 开关问题</h3><p>8.10 难度2<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>如果把异或看做是不进位的加法，那么就很好理解了<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">namespace</span> mine</div><div class="line">&#123;</div><div class="line">    <span class="keyword">const</span> <span class="keyword">int</span> MAXN=<span class="number">40</span>;</div><div class="line">    <span class="keyword">int</span> n;</div><div class="line">    <span class="keyword">int</span> p[MAXN][MAXN];</div><div class="line">    <span class="keyword">int</span> a[MAXN][MAXN],b[MAXN];<span class="comment">//系数、常数</span></div><div class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">iszero</span><span class="params">(<span class="keyword">int</span> i)</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j&lt;=n;j++) <span class="keyword">if</span>(a[i][j]&gt;<span class="number">0</span>) <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">        <span class="keyword">return</span> <span class="number">1</span>;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">int</span> <span class="title">gauss</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">int</span> nx=<span class="number">-1</span>;</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> j=i;j&lt;=n;j++) <span class="keyword">if</span>(a[j][i]&gt;<span class="number">0</span>) &#123;nx=j;<span class="keyword">break</span>;&#125;</div><div class="line">            <span class="keyword">if</span>(nx&lt;<span class="number">0</span>) <span class="keyword">continue</span>;</div><div class="line">            swap(b[i],b[nx]);<span class="keyword">for</span>(<span class="keyword">int</span> k=<span class="number">1</span>;k&lt;=n;k++) swap(a[i][k],a[nx][k]);</div><div class="line"></div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j&lt;=n;j++)</div><div class="line">            &#123;</div><div class="line">                <span class="keyword">if</span>(i==j or !a[j][i]) <span class="keyword">continue</span>;</div><div class="line">                b[j]^=b[i];<span class="keyword">for</span>(<span class="keyword">int</span> k=i;k&lt;=n;k++) a[j][k]^=a[i][k];</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line"></div><div class="line">        <span class="keyword">int</span> tot=<span class="number">0</span>;<span class="comment">//主元</span></div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++)</div><div class="line">            <span class="keyword">if</span>(!iszero(i)) tot++;</div><div class="line">            <span class="keyword">else</span> <span class="keyword">if</span>(b[i]!=<span class="number">0</span>) <span class="keyword">return</span> <span class="number">-1</span>;</div><div class="line">        <span class="keyword">return</span> <span class="number">1</span>&lt;&lt;(n-tot);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">int</span> T;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;T);</div><div class="line">        <span class="keyword">while</span>(T--)</div><div class="line">        &#123;</div><div class="line">            <span class="built_in">memset</span>(a,<span class="number">0</span>,<span class="keyword">sizeof</span> a);</div><div class="line"></div><div class="line">            <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;n);</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++) a[i][i]=<span class="number">1</span>,<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;b[i]);</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++) &#123;<span class="keyword">int</span> t;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;t);b[i]^=t;&#125;</div><div class="line">            <span class="keyword">while</span>(<span class="number">1</span>)</div><div class="line">            &#123;</div><div class="line">                <span class="keyword">int</span> x,y;<span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&amp;x,&amp;y);</div><div class="line">                <span class="keyword">if</span>(x==<span class="number">0</span> and y==<span class="number">0</span>) <span class="keyword">break</span>;</div><div class="line">                a[y][x]=<span class="number">1</span>;<span class="comment">//debug 要反过来</span></div><div class="line">            &#125;</div><div class="line">            <span class="keyword">int</span> ans=gauss();</div><div class="line">            <span class="keyword">if</span>(ans&gt;<span class="number">0</span>) <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,ans);</div><div class="line">            <span class="keyword">else</span> <span class="built_in">puts</span>(<span class="string">"Oh,it's impossible~!!"</span>);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    mine::main();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div></p>
<h3 id="10-JLOI2015-装备购买"><a href="#10-JLOI2015-装备购买" class="headerlink" title="10 JLOI2015 装备购买"></a>10 JLOI2015 装备购买</h3><p>8.11 难度2<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>卡精度，要开long double<br>所以决定以后无脑long double了hh</p>
<p>为了做第二问，贪心地每次找最小的作为主元<br>证明不会，但是我构造不出反例<br>本来以为找到一个：<br>3 2<br>3 0<br>4 4<br>5 0<br>2 1 3<br>但其实，经过消元，第一行不会是0<br>如果非要是0，那么第二行一定是第一行的倍数，则也能反过来……（感觉我表述得乱七八糟，意会一下吧）<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">namespace</span> mine</div><div class="line">&#123;</div><div class="line">    <span class="meta">#<span class="meta-keyword">define</span> double long double</span></div><div class="line">    <span class="function"><span class="keyword">double</span> <span class="title">myabs</span><span class="params">(<span class="keyword">double</span> x)</span> </span>&#123;<span class="keyword">return</span> x&gt;<span class="number">0</span>?x:-x;&#125;</div><div class="line">    <span class="keyword">const</span> <span class="keyword">double</span> eps=<span class="number">1e-8</span>;</div><div class="line"></div><div class="line">    <span class="keyword">int</span> n,m;</div><div class="line">    <span class="keyword">double</span> a[<span class="number">510</span>][<span class="number">510</span>],c[<span class="number">510</span>];</div><div class="line">    <span class="keyword">int</span> tot=<span class="number">0</span>;<span class="keyword">double</span> sum=<span class="number">0</span>;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">gauss</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=m;i++)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">int</span> nx=<span class="number">-1</span>;</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> j=tot+<span class="number">1</span>;j&lt;=n;j++)</div><div class="line">                <span class="keyword">if</span>(myabs(a[j][i])&gt;eps)</div><div class="line">                    <span class="keyword">if</span>(nx&lt;<span class="number">0</span> or c[nx]&gt;c[j]) nx=j;</div><div class="line">            <span class="keyword">if</span>(nx&lt;<span class="number">0</span>) <span class="keyword">continue</span>;</div><div class="line">            tot++;sum+=c[nx];</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> k=<span class="number">1</span>;k&lt;=m;k++) swap(a[tot][k],a[nx][k]);</div><div class="line">            swap(c[tot],c[nx]);<span class="comment">//debug 漏了</span></div><div class="line"></div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j&lt;=n;j++)</div><div class="line">            &#123;</div><div class="line">                <span class="keyword">if</span>(tot==j or myabs(a[j][i])&lt;=eps) <span class="keyword">continue</span>;</div><div class="line">                <span class="keyword">double</span> t=a[j][i]/a[tot][i];</div><div class="line">                <span class="keyword">for</span>(<span class="keyword">int</span> k=i;k&lt;=m;k++) a[j][k]-=a[tot][k]*t;</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&amp;n,&amp;m);</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++)</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j&lt;=m;j++)</div><div class="line">                <span class="built_in">scanf</span>(<span class="string">"%Lf"</span>,&amp;a[i][j]);</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++) <span class="built_in">scanf</span>(<span class="string">"%Lf"</span>,&amp;c[i]);</div><div class="line">        gauss();</div><div class="line">        <span class="built_in">printf</span>(<span class="string">"%d %.0Lf"</span>,tot,sum);</div><div class="line">    &#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    mine::main();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div></p>
<h3 id="11-Hdu3949-XOR"><a href="#11-Hdu3949-XOR" class="headerlink" title="11 Hdu3949 XOR"></a>11 Hdu3949 XOR</h3><p>8.11 难度2<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>key：<br>高斯消元后，得到的简化阶梯形矩阵，具有一个重要的性质————对于主元i，该列上唯一的1在这上面<br>所以说，在其他相同的情况下，选i一定比不选要大<br>所以说，可以把k按二进制拆分，按位，对应于要不要选择第i行</p>
<p>细节：<br>注意0，线性基可以通过【自己异或自己】得出，但本题不行<br>所以说特判一下最后的矩阵，就好了，具体自己分析</p>
<p>最后补充一下【异或线性基组合出来的数（即span张成）互不相同】的证明：<br>假设有一个柿子，左右两边都是异或出来的数字，把左边留下某一个，<br>其他移项到右边，那么出现，那个数字能被其他数字表示出，则违反线性基定义</p>
<p>顺便说说异或能移项的证明：把两边同时异或那个数</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">namespace</span> mine</div><div class="line">&#123;</div><div class="line">    <span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</div><div class="line"></div><div class="line">    ll bin[<span class="number">70</span>];</div><div class="line">    <span class="keyword">int</span> n;</div><div class="line">    <span class="keyword">bool</span> zero;</div><div class="line">    ll a[<span class="number">11000</span>];</div><div class="line">    <span class="keyword">int</span> tot;<span class="comment">//线性基长度</span></div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">gauss</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        zero=<span class="number">0</span>;tot=<span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">60</span>;i&gt;=<span class="number">0</span>;i--)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">int</span> nx=tot+<span class="number">1</span>;</div><div class="line">            <span class="keyword">while</span>( !(a[nx]&amp;bin[i]) and nx&lt;=n ) nx++;</div><div class="line">            <span class="keyword">if</span>(nx&gt;n) <span class="keyword">continue</span>;</div><div class="line"></div><div class="line">            tot++;swap(a[tot],a[nx]);</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> k=<span class="number">1</span>;k&lt;=n;k++)</div><div class="line">                <span class="keyword">if</span>(k!=tot and a[k]&amp;bin[i])</div><div class="line">                    a[k]^=a[tot];</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">if</span>(tot&lt;n) zero=<span class="number">1</span>;</div><div class="line">    &#125;</div><div class="line">    <span class="function">ll <span class="title">solve</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        ll k;<span class="built_in">scanf</span>(<span class="string">"%lld"</span>,&amp;k);</div><div class="line"></div><div class="line">        <span class="keyword">if</span>(zero) k--;<span class="comment">//能产生0时，k=1得0，k=2得最小</span></div><div class="line">        <span class="keyword">if</span>(k&gt;bin[tot]<span class="number">-1</span>) <span class="keyword">return</span> <span class="number">-1</span>;<span class="comment">//去除【什么都不选的情况】</span></div><div class="line"></div><div class="line">        ll ans=<span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;=tot<span class="number">-1</span>;i++) <span class="keyword">if</span>(k&amp;bin[i]) ans^=a[tot-i];</div><div class="line">        <span class="keyword">return</span> ans;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        bin[<span class="number">0</span>]=<span class="number">1</span>;<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=<span class="number">60</span>;i++) bin[i]=bin[i<span class="number">-1</span>]&lt;&lt;<span class="number">1</span>;</div><div class="line"></div><div class="line">        <span class="keyword">int</span> T;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;T);</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> ct=<span class="number">1</span>;ct&lt;=T;ct++)</div><div class="line">        &#123;</div><div class="line">            <span class="built_in">printf</span>(<span class="string">"Case #%d:\n"</span>,ct);</div><div class="line"></div><div class="line">            <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;n);</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++) <span class="built_in">scanf</span>(<span class="string">"%lld"</span>,&amp;a[i]);</div><div class="line">			gauss();</div><div class="line"></div><div class="line">            <span class="keyword">int</span> q;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;q);</div><div class="line">            <span class="keyword">while</span>(q--) <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>,solve());</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    mine::main();</div><div class="line">&#125;</div></pre></td></tr></table></figure>

</div></div></p>
<h3 id="12-3602-Counting-Swaps"><a href="#12-3602-Counting-Swaps" class="headerlink" title="12 3602 Counting Swaps"></a>12 3602 Counting Swaps</h3><p>8.13 难度3<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p><a href="b51f.html">多重集组合数</a></p>
<p><img src="../images/算法竞赛进阶指南/Counting_Swaps.jpg" alt=""></p>
<p>假如写出前面的几个数（谁会这样啊……都退出柿子了，也好像化简不出什么）：<br>1,1,3,16,125<br>会发现$s[n]=n^{n-2}$</p>
<p>对于k个长度分别为l1,l2,…,lk的环<br>$ans=\prod s[l_1]<em>s[l_2]</em>…<em>s[l_k]</em>\frac{(n-k)!}{(l_1-1)!(l_2-1)!…(l_k-1)!}$<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">namespace</span> mine</div><div class="line">&#123;</div><div class="line">    <span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</div><div class="line"></div><div class="line">    <span class="keyword">const</span> <span class="keyword">int</span> MAXN=<span class="number">110000</span>;</div><div class="line">    <span class="keyword">const</span> <span class="keyword">int</span> MOD=<span class="number">1e9</span>+<span class="number">9</span>;</div><div class="line">    ll fac[MAXN];</div><div class="line"></div><div class="line">    <span class="function">ll <span class="title">qpower</span><span class="params">(ll a,<span class="keyword">int</span> e)</span></div><div class="line">    </span>&#123;</div><div class="line">        ll ans=<span class="number">1</span>;</div><div class="line">        <span class="keyword">while</span>(e&gt;<span class="number">0</span>)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">if</span>(e&amp;<span class="number">1</span>) ans=ans*a%MOD;</div><div class="line">            a=a*a%MOD;e&gt;&gt;=<span class="number">1</span>;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> ans;</div><div class="line">    &#125;</div><div class="line">    <span class="function">ll <span class="title">exgcd</span><span class="params">(ll a,ll b,ll &amp;x,ll &amp;y)</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">if</span>(b==<span class="number">0</span>)</div><div class="line">        &#123;</div><div class="line">            x=<span class="number">1</span>;y=<span class="number">0</span>;</div><div class="line">            <span class="keyword">return</span> a;</div><div class="line">        &#125;</div><div class="line">        ll tx,ty;</div><div class="line">        ll d=exgcd(b,a%b,tx,ty);</div><div class="line">        x=ty;</div><div class="line">        y=tx-(a/b)*ty;</div><div class="line">        <span class="keyword">return</span> d;</div><div class="line">    &#125;</div><div class="line">    <span class="function">ll <span class="title">inv</span><span class="params">(ll a)</span></div><div class="line">    </span>&#123;</div><div class="line">        ll x,y;</div><div class="line">        exgcd(a,MOD,x,y);</div><div class="line">        <span class="keyword">return</span> (x%MOD+MOD)%MOD;</div><div class="line">    &#125;</div><div class="line"></div><div class="line"></div><div class="line">    <span class="keyword">int</span> p[MAXN];</div><div class="line">    <span class="keyword">bool</span> v[MAXN];</div><div class="line">    <span class="function"><span class="keyword">int</span> <span class="title">getsiz</span><span class="params">(<span class="keyword">int</span> x)</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">int</span> now=p[x],tot=<span class="number">1</span>;</div><div class="line">        v[x]=<span class="number">1</span>;</div><div class="line">        <span class="keyword">while</span>(now!=x)</div><div class="line">        &#123;</div><div class="line">            v[now]=<span class="number">1</span>;</div><div class="line">            tot++;</div><div class="line">            now=p[now];</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> tot;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        fac[<span class="number">0</span>]=<span class="number">1</span>;<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;MAXN;i++) fac[i]=fac[i<span class="number">-1</span>]*i%MOD;</div><div class="line"></div><div class="line">        <span class="keyword">int</span> T;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;T);</div><div class="line">        <span class="keyword">while</span>(T--)</div><div class="line">        &#123;</div><div class="line">            <span class="built_in">memset</span>(v,<span class="number">0</span>,<span class="keyword">sizeof</span> v);</div><div class="line"></div><div class="line">            <span class="keyword">int</span> n;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;n);</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++) <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;p[i]);</div><div class="line"></div><div class="line">            ll ans=<span class="number">1</span>;<span class="keyword">int</span> tot=<span class="number">0</span>;</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++)</div><div class="line">                <span class="keyword">if</span>(!v[i])</div><div class="line">                &#123;</div><div class="line">                    <span class="keyword">int</span> l=getsiz(i);tot++;</div><div class="line">                    ans=ans*qpower(l,l<span class="number">-2</span>)%MOD;</div><div class="line">                    ans=ans*inv(fac[l<span class="number">-1</span>])%MOD;</div><div class="line">                &#125;</div><div class="line">            <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>,ans*fac[n-tot]%MOD);</div><div class="line">        &#125;</div><div class="line"></div><div class="line">    &#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    mine::main();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div></p>
<h3 id="13-CF451E-Devu-and-Flowers"><a href="#13-CF451E-Devu-and-Flowers" class="headerlink" title="13 CF451E Devu and Flowers"></a>13 CF451E Devu and Flowers</h3><p>8.16 难度2<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>多重集组合数裸题，<a href="b51f.html">讲解</a><br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">namespace</span> mine</div><div class="line">&#123;</div><div class="line">    <span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</div><div class="line">    <span class="keyword">const</span> ll MOD=<span class="number">1e9</span>+<span class="number">7</span>;</div><div class="line"></div><div class="line">    <span class="function">ll <span class="title">qpower</span><span class="params">(ll x,ll e)</span></div><div class="line">    </span>&#123;</div><div class="line">        ll ans=<span class="number">1</span>;</div><div class="line">        <span class="keyword">while</span>(e&gt;<span class="number">0</span>)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">if</span>(e&amp;<span class="number">1</span>) ans=ans*x%MOD;</div><div class="line">            x=x*x%MOD;e&gt;&gt;=<span class="number">1</span>;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> ans;</div><div class="line">    &#125;</div><div class="line">    <span class="function">ll <span class="title">inv</span><span class="params">(ll x)</span> </span>&#123;<span class="keyword">return</span> qpower(x,MOD<span class="number">-2</span>);&#125;</div><div class="line"></div><div class="line">    <span class="function">ll <span class="title">C</span><span class="params">(ll m,ll n)</span><span class="comment">//O(m)</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">if</span>(m&lt;<span class="number">0</span> or n&lt;<span class="number">0</span> or m&gt;n) <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">        ll ans=<span class="number">1</span>;</div><div class="line">        <span class="keyword">for</span>(ll t=n;t&gt;=n-m+<span class="number">1</span>;t--) ans=ans*(t%MOD)%MOD;</div><div class="line">        ll s=<span class="number">1</span>;<span class="keyword">for</span>(ll t=<span class="number">2</span>;t&lt;=m;t++) s=s*t%MOD;</div><div class="line">        <span class="keyword">return</span> ans*inv(s)%MOD;</div><div class="line">    &#125;</div><div class="line">    ll a[<span class="number">30</span>];</div><div class="line">    <span class="keyword">int</span> bin[<span class="number">30</span>];</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        bin[<span class="number">0</span>]=<span class="number">1</span>;<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;<span class="number">30</span>;i++) bin[i]=bin[i<span class="number">-1</span>]&lt;&lt;<span class="number">1</span>;</div><div class="line"></div><div class="line">        <span class="keyword">int</span> n;ll m;<span class="built_in">scanf</span>(<span class="string">"%d%I64d"</span>,&amp;n,&amp;m);</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++) <span class="built_in">scanf</span>(<span class="string">"%I64d"</span>,&amp;a[i]);</div><div class="line"></div><div class="line">        ll ans=<span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> s=<span class="number">0</span>;s&lt;=bin[n]<span class="number">-1</span>;s++)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">int</span> num=<span class="number">0</span>;</div><div class="line">            ll M=n<span class="number">-1</span>,N=(ll)n<span class="number">-1</span>+m;<span class="comment">//debug n转longlong</span></div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;=n<span class="number">-1</span>;i++) <span class="keyword">if</span>(s&amp;bin[i]) N-=a[i+<span class="number">1</span>]+<span class="number">1</span>,num++;<span class="comment">//debug a[i]</span></div><div class="line">            <span class="keyword">if</span>(num&amp;<span class="number">1</span>) ans-=C(M,N);</div><div class="line">            <span class="keyword">else</span> ans+=C(M,N);</div><div class="line">            ans%=MOD;</div><div class="line">        &#125;</div><div class="line">        <span class="built_in">printf</span>(<span class="string">"%I64d"</span>,(ans+MOD)%MOD);</div><div class="line">    &#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    mine::main();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div></p>
<h3 id="14-POI2007-ZAP-Queries"><a href="#14-POI2007-ZAP-Queries" class="headerlink" title="14 POI2007 ZAP-Queries"></a>14 POI2007 ZAP-Queries</h3><p>8.16 难度1<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>莫比乌斯反演裸题<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">namespace</span> mine</div><div class="line">&#123;</div><div class="line">    <span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</div><div class="line"></div><div class="line">    <span class="keyword">const</span> <span class="keyword">int</span> MAXNUM=<span class="number">51000</span>;</div><div class="line">    <span class="keyword">bool</span> v[MAXNUM];</div><div class="line">    <span class="keyword">int</span> pr=<span class="number">0</span>,prime[MAXNUM];</div><div class="line">    <span class="keyword">int</span> mu[MAXNUM];</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">pre</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        mu[<span class="number">1</span>]=<span class="number">1</span>;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">2</span>;i&lt;MAXNUM;i++)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">if</span>(!v[i]) prime[++pr]=i,mu[i]=<span class="number">-1</span>;</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j&lt;=pr and (ll)i*prime[j]&lt;MAXNUM;j++)</div><div class="line">            &#123;</div><div class="line">                v[i*prime[j]]=<span class="number">1</span>;</div><div class="line">                <span class="keyword">if</span>(i%prime[j]==<span class="number">0</span>) &#123;mu[i*prime[j]]=<span class="number">0</span>;<span class="keyword">break</span>;&#125;</div><div class="line">                mu[i*prime[j]]=-mu[i];</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">2</span>;i&lt;MAXNUM;i++) mu[i]+=mu[i<span class="number">-1</span>];</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">int</span> <span class="title">mymin</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span> </span>&#123;<span class="keyword">return</span> x&lt;y?x:y;&#125;</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        pre();</div><div class="line"></div><div class="line">        <span class="keyword">int</span> q;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;q);</div><div class="line">        <span class="keyword">while</span>(q--)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">int</span> a,b,d;<span class="built_in">scanf</span>(<span class="string">"%d%d%d"</span>,&amp;a,&amp;b,&amp;d);</div><div class="line">            <span class="keyword">int</span> n=a/d,m=b/d;<span class="keyword">if</span>(n&gt;m) swap(n,m);</div><div class="line"></div><div class="line">            ll ans=<span class="number">0</span>;</div><div class="line">            <span class="keyword">for</span>(ll d=<span class="number">1</span>;d&lt;=n;)</div><div class="line">            &#123;</div><div class="line">                <span class="keyword">int</span> lst=mymin(n/(n/d),m/(m/d));</div><div class="line">                ans+=ll(mu[lst]-mu[d<span class="number">-1</span>])*(n/d)*(m/d);</div><div class="line">                d=lst+<span class="number">1</span>;</div><div class="line">            &#125;</div><div class="line">            <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>,ans);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    mine::main();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div></p>
<h3 id="15-3801-Rainbow的信号"><a href="#15-3801-Rainbow的信号" class="headerlink" title="15 3801    Rainbow的信号"></a>15 3801    Rainbow的信号</h3><p>8.16 难度2<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>求<a href="7366.html">期望</a><br>位运算的特性在于，不同的二进制位之间没有影响<br>所以为了方便统计，先枚举每一个位，然后分类讨论</p>
<p>线性枚举r<br>①and<br>如果出现了0，就是0<br>那么最后一个0把前面l的取值范围分成两半，其中只有后面的部分有贡献<br>所以记录一下lst0<br>②or<br>如果出现1，就是1<br>和and同理，记录lst1<br>③xor<br>这个稍微麻烦一些，因为取值涉及到1出现个数的奇偶性<br>那么把按照1的出现次数，分奇偶，形成交替的区间<br>那么只有贡献为奇数的区间是有用的<br>用c0、c1分别记录偶数区间和奇数区间的总长度即可<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">namespace</span> mine</div><div class="line">&#123;</div><div class="line">    <span class="meta">#<span class="meta-keyword">define</span> double long double</span></div><div class="line">    <span class="keyword">int</span> bin[<span class="number">40</span>];</div><div class="line">    <span class="keyword">int</span> a[<span class="number">110000</span>];</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        bin[<span class="number">0</span>]=<span class="number">1</span>;<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=<span class="number">30</span>;i++) bin[i]=bin[i<span class="number">-1</span>]&lt;&lt;<span class="number">1</span>;</div><div class="line"></div><div class="line">        <span class="keyword">int</span> n;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;n);</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++) <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;a[i]);</div><div class="line"></div><div class="line">        <span class="keyword">double</span> xor1=<span class="number">0</span>,and1=<span class="number">0</span>,or1=<span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> k=<span class="number">0</span>;k&lt;=<span class="number">29</span>;k++)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">int</span> lst0=<span class="number">0</span>,lst1=<span class="number">0</span>;<span class="comment">//上一个</span></div><div class="line">            <span class="keyword">int</span> c1=<span class="number">0</span>,c0=<span class="number">0</span>;<span class="comment">//奇偶区间</span></div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> r=<span class="number">1</span>;r&lt;=n;r++)</div><div class="line">            &#123;</div><div class="line">                <span class="keyword">int</span> now=a[r]&amp;bin[k];</div><div class="line"></div><div class="line">                <span class="comment">//l=r</span></div><div class="line">                <span class="keyword">if</span>(now) xor1+=bin[k],and1+=bin[k],or1+=bin[k];</div><div class="line"></div><div class="line">                <span class="comment">//l&lt;r</span></div><div class="line">                <span class="keyword">if</span>(now)</div><div class="line">                &#123;</div><div class="line">                    xor1+=<span class="number">2.0</span>*c0*bin[k];</div><div class="line">                    and1+=<span class="number">2.0</span>*(r-lst0<span class="number">-1</span>)*bin[k];<span class="comment">//lst0+1~r</span></div><div class="line">                    or1+=<span class="number">2.0</span>*(r<span class="number">-1</span>)*bin[k];<span class="comment">//1~lst1</span></div><div class="line">                &#125;</div><div class="line">                <span class="keyword">else</span></div><div class="line">                &#123;</div><div class="line">                    xor1+=<span class="number">2.0</span>*c1*bin[k];</div><div class="line">                    or1+=<span class="number">2.0</span>*lst1*bin[k];<span class="comment">//1~lst1</span></div><div class="line">                &#125;</div><div class="line"></div><div class="line">                <span class="comment">//update</span></div><div class="line">                <span class="keyword">if</span>(now) lst1=r; <span class="keyword">else</span> lst0=r;</div><div class="line">                <span class="keyword">if</span>(now) swap(c1,c0),c1++; <span class="keyword">else</span> c0++;</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">        <span class="built_in">printf</span>(<span class="string">"%.3Lf %.3Lf %.3Lf"</span>,xor1/n/n,and1/n/n,or1/n/n);</div><div class="line">    &#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    mine::main();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div></p>
<h3 id="16-3802-绿豆蛙的归宿"><a href="#16-3802-绿豆蛙的归宿" class="headerlink" title="16 3802    绿豆蛙的归宿"></a>16 3802    绿豆蛙的归宿</h3><p>8.16 难度2<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p><a href="7366.html">期望的线性性</a><br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">namespace</span> mine</div><div class="line">&#123;</div><div class="line">    <span class="keyword">const</span> <span class="keyword">int</span> MAXN=<span class="number">110000</span>,MAXM=<span class="number">210000</span>;</div><div class="line">    <span class="keyword">struct</span> Nod</div><div class="line">    &#123;</div><div class="line">        <span class="keyword">int</span> hou;</div><div class="line">        <span class="keyword">int</span> ru;</div><div class="line">        <span class="keyword">int</span> backup;</div><div class="line">        Nod()</div><div class="line">        &#123;</div><div class="line">            hou=ru=<span class="number">0</span>;</div><div class="line">        &#125;</div><div class="line">    &#125;p[MAXN];</div><div class="line">    <span class="keyword">struct</span> Edge</div><div class="line">    &#123;</div><div class="line">        <span class="keyword">int</span> y,c,g;</div><div class="line">    &#125;e[MAXM];</div><div class="line">    <span class="keyword">int</span> ln=<span class="number">0</span>;</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">ins</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y,<span class="keyword">int</span> c)</span></div><div class="line">    </span>&#123;</div><div class="line">        ln++;</div><div class="line">        e[ln].y=y;e[ln].c=c;</div><div class="line">        e[ln].g=p[x].hou;p[x].hou=ln;</div><div class="line">        p[y].ru++;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">double</span> E[MAXM];</div><div class="line">    <span class="keyword">int</span> sta[MAXN];</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">topsort</span><span class="params">(<span class="keyword">int</span> st)</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">int</span> top=<span class="number">0</span>;sta[++top]=st;</div><div class="line">        <span class="keyword">while</span>(top&gt;<span class="number">0</span>)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">int</span> x=sta[top--];</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> k=p[x].hou;k&gt;<span class="number">0</span>;k=e[k].g)</div><div class="line">            &#123;</div><div class="line">                <span class="keyword">int</span> y=e[k].y;</div><div class="line">                p[y].ru--;</div><div class="line">                E[y]+=(E[x]+e[k].c)/p[y].backup;</div><div class="line">                <span class="keyword">if</span>(!p[y].ru) sta[++top]=y;</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">int</span> n,m;<span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&amp;n,&amp;m);</div><div class="line">        <span class="keyword">while</span>(m--)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">int</span> a,b,c;</div><div class="line">            <span class="built_in">scanf</span>(<span class="string">"%d%d%d"</span>,&amp;a,&amp;b,&amp;c);</div><div class="line">            ins(b,a,c);</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++) p[i].backup=p[i].ru;</div><div class="line">        topsort(n);</div><div class="line">        <span class="built_in">printf</span>(<span class="string">"%.2lf"</span>,E[<span class="number">1</span>]);</div><div class="line">    &#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    mine::main();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div></p>
<h3 id="17-3803-扑克牌"><a href="#17-3803-扑克牌" class="headerlink" title="17 3803    扑克牌"></a>17 3803    扑克牌</h3><p>8.16 难度2<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>本来写的是倒着dp，好像被卡边界了<br>结果tm改成记忆化搜索就过了？？<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">namespace</span> mine</div><div class="line">&#123;</div><div class="line">    <span class="function"><span class="keyword">double</span> <span class="title">mymin</span><span class="params">(<span class="keyword">double</span> x,<span class="keyword">double</span> y)</span> </span>&#123;<span class="keyword">return</span> x&lt;y?x:y;&#125;</div><div class="line"></div><div class="line">    <span class="keyword">int</span> A,B,C,D;</div><div class="line">    <span class="keyword">double</span> f[<span class="number">20</span>][<span class="number">20</span>][<span class="number">20</span>][<span class="number">20</span>][<span class="number">5</span>][<span class="number">5</span>];</div><div class="line">    <span class="keyword">bool</span> v[<span class="number">20</span>][<span class="number">20</span>][<span class="number">20</span>][<span class="number">20</span>][<span class="number">5</span>][<span class="number">5</span>];</div><div class="line">    <span class="function"><span class="keyword">double</span> <span class="title">dp</span><span class="params">(<span class="keyword">int</span> a,<span class="keyword">int</span> b,<span class="keyword">int</span> c,<span class="keyword">int</span> d,<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">if</span>(v[a][b][c][d][x][y]) <span class="keyword">return</span> f[a][b][c][d][x][y];</div><div class="line">        v[a][b][c][d][x][y]=<span class="number">1</span>;</div><div class="line"></div><div class="line">        <span class="keyword">double</span> &amp;ans=f[a][b][c][d][x][y];</div><div class="line">        <span class="keyword">int</span> aa=a+(x==<span class="number">1</span>)+(y==<span class="number">1</span>),bb=b+(x==<span class="number">2</span>)+(y==<span class="number">2</span>),cc=c+(x==<span class="number">3</span>)+(y==<span class="number">3</span>),dd=d+(x==<span class="number">4</span>)+(y==<span class="number">4</span>);</div><div class="line">        <span class="keyword">if</span>(aa&gt;=A and bb&gt;=B and cc&gt;=C and dd&gt;=D) <span class="keyword">return</span> ans=<span class="number">0</span>;</div><div class="line"></div><div class="line">        <span class="keyword">int</span> left=<span class="number">54</span>-aa-bb-cc-dd;</div><div class="line">        <span class="keyword">if</span>(a&lt;<span class="number">13</span>) ans+=(dp(a+<span class="number">1</span>,b,c,d,x,y)+<span class="number">1</span>)*(<span class="number">13</span>-a)/left;</div><div class="line">        <span class="keyword">if</span>(b&lt;<span class="number">13</span>) ans+=(dp(a,b+<span class="number">1</span>,c,d,x,y)+<span class="number">1</span>)*(<span class="number">13</span>-b)/left;</div><div class="line">        <span class="keyword">if</span>(c&lt;<span class="number">13</span>) ans+=(dp(a,b,c+<span class="number">1</span>,d,x,y)+<span class="number">1</span>)*(<span class="number">13</span>-c)/left;</div><div class="line">        <span class="keyword">if</span>(d&lt;<span class="number">13</span>) ans+=(dp(a,b,c,d+<span class="number">1</span>,x,y)+<span class="number">1</span>)*(<span class="number">13</span>-d)/left;</div><div class="line"></div><div class="line">        <span class="keyword">if</span>(x==<span class="number">0</span>) &#123;<span class="keyword">double</span> mi=<span class="number">1000</span>;<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=<span class="number">4</span>;i++) &#123;mi=mymin(mi,dp(a,b,c,d,i,y));&#125;ans+=(mi+<span class="number">1</span>)/left;&#125;</div><div class="line">        <span class="keyword">if</span>(y==<span class="number">0</span>) &#123;<span class="keyword">double</span> mi=<span class="number">1000</span>;<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=<span class="number">4</span>;i++) &#123;mi=mymin(mi,dp(a,b,c,d,x,i));&#125;ans+=(mi+<span class="number">1</span>)/left;&#125;</div><div class="line"></div><div class="line">        <span class="keyword">return</span> ans;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="built_in">scanf</span>(<span class="string">"%d%d%d%d"</span>,&amp;A,&amp;B,&amp;C,&amp;D);</div><div class="line">        <span class="keyword">int</span> p1=(A&gt;<span class="number">13</span>)?A<span class="number">-13</span>:<span class="number">0</span>,p2=(B&gt;<span class="number">13</span>)?B<span class="number">-13</span>:<span class="number">0</span>,p3=(C&gt;<span class="number">13</span>)?C<span class="number">-13</span>:<span class="number">0</span>,p4=(D&gt;<span class="number">13</span>)?D<span class="number">-13</span>:<span class="number">0</span>;</div><div class="line">        <span class="keyword">if</span>(p1+p2+p3+p4&gt;<span class="number">2</span>) <span class="built_in">puts</span>(<span class="string">"-1.000"</span>);</div><div class="line">        <span class="keyword">else</span> <span class="built_in">printf</span>(<span class="string">"%.3lf"</span>,dp(<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>));</div><div class="line">    &#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    mine::main();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div></p>
<h3 id="18-poj2311-Cutting-Game"><a href="#18-poj2311-Cutting-Game" class="headerlink" title="18 poj2311 Cutting Game"></a>18 poj2311 Cutting Game</h3><p>8.17 难度2<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p><a href="c7bb.html">博弈</a><br>不过，如果出现(1,1)，并定义为必败态，本身没有问题<br>但考虑只有一行或一列的情况，会把两个有向图游戏异或起来，<br>然后对方就会“努力翻盘”，导致本来游戏早就结束，却被“莫名其妙翻盘了”<br>所以要跳过这些状态，并早点判断出必败态：(2,2)和(2,3)<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">namespace</span> mine</div><div class="line">&#123;</div><div class="line">    <span class="keyword">int</span> sg[<span class="number">210</span>][<span class="number">210</span>];</div><div class="line">    <span class="keyword">bool</span> v[<span class="number">210</span>][<span class="number">210</span>];</div><div class="line"></div><div class="line">    <span class="keyword">bool</span> b[<span class="number">410</span>];<span class="comment">//每次产生不同，最多只有400个</span></div><div class="line">    <span class="function"><span class="keyword">int</span> <span class="title">SG</span><span class="params">(<span class="keyword">int</span> n,<span class="keyword">int</span> m)</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">if</span>(n&gt;m) swap(n,m);</div><div class="line">        <span class="keyword">if</span>(v[n][m]) <span class="keyword">return</span> sg[n][m];</div><div class="line">        v[n][m]=<span class="number">1</span>;</div><div class="line">        <span class="keyword">if</span>(n==<span class="number">1</span> and m==<span class="number">1</span>) <span class="keyword">return</span> sg[n][m]=<span class="number">0</span>;</div><div class="line">        <span class="comment">//if(n==2 and m==2) return sg[n][m]=0;</span></div><div class="line">        <span class="comment">//if(n==2 and m==3) return sg[n][m]=0;</span></div><div class="line"></div><div class="line">        <span class="built_in">memset</span>(b,<span class="number">0</span>,<span class="keyword">sizeof</span> b);</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> k=<span class="number">2</span>;k&lt;=n<span class="number">-2</span>;k++) b[ SG(k,m)^SG(n-k,m) ]=<span class="number">1</span>;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> k=<span class="number">2</span>;k&lt;=m<span class="number">-2</span>;k++) b[ SG(n,k)^SG(n,m-k) ]=<span class="number">1</span>;</div><div class="line"></div><div class="line">        <span class="keyword">int</span> now=<span class="number">0</span>;<span class="keyword">while</span>(b[now]) now++;</div><div class="line">        <span class="keyword">return</span> sg[n][m]=now;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">int</span> n,m;</div><div class="line">        <span class="keyword">while</span>(<span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&amp;n,&amp;m)!=EOF)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">if</span>(SG(n,m)&gt;<span class="number">0</span>) <span class="built_in">puts</span>(<span class="string">"WIN"</span>);</div><div class="line">            <span class="keyword">else</span> <span class="built_in">puts</span>(<span class="string">"LOSE"</span>);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    mine::main();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div></p>
<h2 id="0x3B-数学练习"><a href="#0x3B-数学练习" class="headerlink" title="0x3B 数学练习"></a>0x3B 数学练习</h2><p><a href="http://contest-hunter.org:83/contest/0x30%E3%80%8C%E6%95%B0%E5%AD%A6%E7%9F%A5%E8%AF%86%E3%80%8D%E4%BE%8B%E9%A2%98" target="_blank" rel="noopener">题目</a></p>
<h3 id="19-SDOI2012-Longge的问题"><a href="#19-SDOI2012-Longge的问题" class="headerlink" title="19 SDOI2012 Longge的问题"></a>19 SDOI2012 Longge的问题</h3><p>8.17 难度2<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>按照反素数的思路，只考虑前面几个素数，按照约数个数公式dfs<br>最终得出，在int范围内约数最多约1500个</p>
<p>那么，如果枚举约数s，把它作为gcd的结果<br>$\sum s \times 【gcd(1 \to n,n)=s的个数】$<br>$\sum s \times 【gcd(1 \to n/s)=1的个数】$<br>$\sum s \times \varphi (n/s)$<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">namespace</span> mine</div><div class="line">&#123;</div><div class="line">    <span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</div><div class="line">    <span class="function">ll <span class="title">phi</span><span class="params">(ll x)</span></div><div class="line">    </span>&#123;</div><div class="line">        ll ans=x;</div><div class="line">        <span class="keyword">for</span>(ll t=<span class="number">2</span>;t*t&lt;=x;t++)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">if</span>(x%t!=<span class="number">0</span>) <span class="keyword">continue</span>;</div><div class="line">            <span class="keyword">while</span>(x%t==<span class="number">0</span>) x/=t;</div><div class="line">            ans=ans/t*(t<span class="number">-1</span>);</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">if</span>(x&gt;<span class="number">1</span>) ans=ans/x*(x<span class="number">-1</span>);</div><div class="line">        <span class="keyword">return</span> ans;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        ll n;<span class="built_in">scanf</span>(<span class="string">"%lld"</span>,&amp;n);</div><div class="line">        ll ans=<span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span>(ll k=<span class="number">1</span>;k*k&lt;=n;k++)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">if</span>(n%k!=<span class="number">0</span>) <span class="keyword">continue</span>;</div><div class="line">            ans+=k*phi(n/k);</div><div class="line">            <span class="keyword">if</span>(k*k!=n) ans+=n/k*phi(k);</div><div class="line">        &#125;</div><div class="line">        <span class="built_in">printf</span>(<span class="string">"%lld"</span>,ans);</div><div class="line">    &#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    mine::main();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div></p>
<h3 id="20-bzoj1477-poj1061-青蛙的约会"><a href="#20-bzoj1477-poj1061-青蛙的约会" class="headerlink" title="20 bzoj1477 poj1061    青蛙的约会"></a>20 bzoj1477 poj1061    青蛙的约会</h3><p>8.17 难度1<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>同余方程组裸题<br>t(n-m)+kL=x-y<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">namespace</span> mine</div><div class="line">&#123;</div><div class="line">    <span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</div><div class="line">    <span class="function">ll <span class="title">exgcd</span><span class="params">(ll a,ll b,ll &amp;x,ll &amp;y)</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">if</span>(b==<span class="number">0</span>) &#123;x=<span class="number">1</span>,y=<span class="number">0</span>;<span class="keyword">return</span> a;&#125;</div><div class="line">        ll tx,ty;ll d=exgcd(b,a%b,tx,ty);</div><div class="line">        x=ty;y=tx-(a/b)*ty;</div><div class="line">        <span class="keyword">return</span> d;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        ll x,y,m,n,L;<span class="built_in">scanf</span>(<span class="string">"%lld%lld%lld%lld%lld"</span>,&amp;x,&amp;y,&amp;m,&amp;n,&amp;L);</div><div class="line">        ll A=n-m,B=L,K=x-y;</div><div class="line"></div><div class="line">        ll tx,ty;</div><div class="line">        ll d=exgcd(A,B,tx,ty);<span class="comment">//t(n-m)+kL=x-y</span></div><div class="line">        <span class="keyword">if</span>(K%d!=<span class="number">0</span>) <span class="built_in">puts</span>(<span class="string">"Impossible"</span>);</div><div class="line">        <span class="keyword">else</span></div><div class="line">        &#123;</div><div class="line">            tx=tx*(K/d);</div><div class="line">            ll t=B/d;</div><div class="line">            <span class="built_in">printf</span>(<span class="string">"%lld"</span>,(tx%t+t)%t);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    mine::main();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div></p>
<h3 id="21-3B04-Xiao-9-大战朱最学"><a href="#21-3B04-Xiao-9-大战朱最学" class="headerlink" title="21 3B04    Xiao 9*大战朱最学"></a>21 3B04    Xiao 9*大战朱最学</h3><p>8.17 难度1<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>同余方程组裸题<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">namespace</span> mine</div><div class="line">&#123;</div><div class="line">    <span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</div><div class="line">    <span class="function">ll <span class="title">exgcd</span><span class="params">(ll a,ll b,ll &amp;x,ll &amp;y)</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">if</span>(b==<span class="number">0</span>) &#123;x=<span class="number">1</span>,y=<span class="number">0</span>;<span class="keyword">return</span> a;&#125;</div><div class="line">        ll tx,ty;ll d=exgcd(b,a%b,tx,ty);</div><div class="line">        x=ty;y=tx-(a/b)*ty;</div><div class="line">        <span class="keyword">return</span> d;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">int</span> n;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;n);</div><div class="line">        ll a1,b1;<span class="built_in">scanf</span>(<span class="string">"%lld%lld"</span>,&amp;a1,&amp;b1);</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">2</span>;i&lt;=n;i++)</div><div class="line">        &#123;</div><div class="line">            ll a2,b2;<span class="built_in">scanf</span>(<span class="string">"%lld%lld"</span>,&amp;a2,&amp;b2);</div><div class="line">            ll A=a1,B=a2,K=b2-b1;</div><div class="line">            ll k1,k2;ll d=exgcd(A,B,k1,k2);</div><div class="line">            k1=k1*(K/d);</div><div class="line">            ll t=B/d;k1=(k1%t+t)%t;</div><div class="line"></div><div class="line">            b1=k1*a1+b1;</div><div class="line">            a1=a1*a2;</div><div class="line">        &#125;</div><div class="line">        <span class="built_in">printf</span>(<span class="string">"%lld"</span>,b1);</div><div class="line">    &#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    mine::main();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div></p>
<h3 id="22-SDOI2011-计算器"><a href="#22-SDOI2011-计算器" class="headerlink" title="22 SDOI2011    计算器"></a>22 SDOI2011    计算器</h3><p>8.17 难度2<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>裸题<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">namespace</span> mine</div><div class="line">&#123;</div><div class="line">    <span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</div><div class="line">    <span class="function">ll <span class="title">qpower</span><span class="params">(ll x,ll e,ll MOD)</span></div><div class="line">    </span>&#123;</div><div class="line">        ll ans=<span class="number">1</span>;x%=MOD;</div><div class="line">        <span class="keyword">while</span>(e&gt;<span class="number">0</span>)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">if</span>(e&amp;<span class="number">1</span>) ans=ans*x%MOD;</div><div class="line">            x=x*x%MOD;e&gt;&gt;=<span class="number">1</span>;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> ans;</div><div class="line">    &#125;</div><div class="line">    <span class="function">ll <span class="title">exgcd</span><span class="params">(ll a,ll b,ll &amp;x,ll &amp;y)</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">if</span>(b==<span class="number">0</span>) &#123;x=<span class="number">1</span>,y=<span class="number">0</span>;<span class="keyword">return</span> a;&#125;</div><div class="line">        ll tx,ty;ll d=exgcd(b,a%b,tx,ty);</div><div class="line">        x=ty;y=tx-(a/b)*ty;</div><div class="line">        <span class="keyword">return</span> d;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">solve2</span><span class="params">(ll A,ll B,ll K)</span></div><div class="line">    </span>&#123;</div><div class="line">        ll x,y;ll d=exgcd(A,B,x,y);</div><div class="line">        <span class="keyword">if</span>(K%d!=<span class="number">0</span>) <span class="built_in">puts</span>(<span class="string">"Orz, I cannot find x!"</span>);</div><div class="line">        <span class="keyword">else</span></div><div class="line">        &#123;</div><div class="line">            x*=(K/d);</div><div class="line">            ll t=B/d;</div><div class="line">            <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>,(x%t+t)%t);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="built_in">map</span>&lt;ll,ll&gt; hash;</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">solve3</span><span class="params">(ll A,ll z,ll MOD)</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">if</span>(A%MOD==<span class="number">0</span>) &#123;<span class="built_in">puts</span>(<span class="string">"Orz, I cannot find x!"</span>);<span class="keyword">return</span>;&#125;<span class="comment">//debug</span></div><div class="line">        A%=MOD;z%=MOD;</div><div class="line"></div><div class="line">        hash.clear();</div><div class="line">        ll t=<span class="built_in">ceil</span>(<span class="built_in">sqrt</span>( (<span class="keyword">double</span>)MOD ));</div><div class="line"></div><div class="line">        ll now=z;<span class="keyword">for</span>(ll b=<span class="number">0</span>;b&lt;=t;b++) hash[now]=b,now=now*A%MOD;</div><div class="line"></div><div class="line">        ll tmp=qpower(A,t,MOD);</div><div class="line">        now=tmp;</div><div class="line">        <span class="keyword">for</span>(ll a=<span class="number">1</span>;a&lt;=t;a++)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">if</span>(hash.count(now)) &#123;<span class="built_in">printf</span>(<span class="string">"%lld\n"</span>,a*t-hash[now]);<span class="keyword">return</span>;&#125;</div><div class="line">            now=now*tmp%MOD;</div><div class="line">        &#125;</div><div class="line">        <span class="built_in">puts</span>(<span class="string">"Orz, I cannot find x!"</span>);</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">int</span> T,k;<span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&amp;T,&amp;k);</div><div class="line">        <span class="keyword">while</span>(T--)</div><div class="line">        &#123;</div><div class="line">            ll y,z,p;<span class="built_in">scanf</span>(<span class="string">"%lld%lld%lld"</span>,&amp;y,&amp;z,&amp;p);</div><div class="line">            <span class="keyword">if</span>(k==<span class="number">1</span>) <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>,qpower(y,z,p));</div><div class="line">            <span class="keyword">else</span> <span class="keyword">if</span>(k==<span class="number">2</span>) solve2(y,p,z);</div><div class="line">            <span class="keyword">else</span> solve3(y,z,p);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    mine::main();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div></p>
<h3 id="23-hdu5015-233-Matrix"><a href="#23-hdu5015-233-Matrix" class="headerlink" title="23 hdu5015 233 Matrix"></a>23 hdu5015 233 Matrix</h3><p>8.17 难度1<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>矩阵乘法裸题<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">namespace</span> mine</div><div class="line">&#123;</div><div class="line">    <span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</div><div class="line">    <span class="keyword">const</span> ll MOD=<span class="number">10000007</span>;</div><div class="line">    <span class="keyword">struct</span> Matrix</div><div class="line">    &#123;</div><div class="line">        ll a[<span class="number">30</span>][<span class="number">30</span>];</div><div class="line">        Matrix() &#123;<span class="built_in">memset</span>(a,<span class="number">0</span>,<span class="keyword">sizeof</span> a);&#125;</div><div class="line">    &#125;;</div><div class="line">    <span class="keyword">const</span> <span class="keyword">int</span> N=<span class="number">20</span>;</div><div class="line">    <span class="function">Matrix <span class="title">pre</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        Matrix ans;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;=N;i++) ans.a[i][i]=<span class="number">1</span>;</div><div class="line">        <span class="keyword">return</span> ans;</div><div class="line">    &#125;</div><div class="line">    <span class="function">Matrix <span class="title">cheng</span><span class="params">(Matrix a,Matrix b)</span></div><div class="line">    </span>&#123;</div><div class="line">        Matrix ans;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;=N;i++)</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;j&lt;=N;j++)</div><div class="line">                <span class="keyword">for</span>(<span class="keyword">int</span> k=<span class="number">0</span>;k&lt;=N;k++)</div><div class="line">                    ans.a[i][j]=(ans.a[i][j]+a.a[i][k]*b.a[k][j]%MOD)%MOD;</div><div class="line">        <span class="keyword">return</span> ans;</div><div class="line">    &#125;</div><div class="line">    <span class="function">Matrix <span class="title">qpower</span><span class="params">(Matrix x,<span class="keyword">int</span> e)</span></div><div class="line">    </span>&#123;</div><div class="line">        Matrix ans=pre();</div><div class="line">        <span class="keyword">while</span>(e&gt;<span class="number">0</span>)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">if</span>(e&amp;<span class="number">1</span>) ans=cheng(ans,x);</div><div class="line">            x=cheng(x,x);e&gt;&gt;=<span class="number">1</span>;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> ans;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">int</span> n,m;</div><div class="line">        <span class="keyword">while</span>(<span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&amp;n,&amp;m)!=EOF)</div><div class="line">        &#123;</div><div class="line">            Matrix st;st.a[<span class="number">0</span>][<span class="number">0</span>]=<span class="number">0</span>;</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++) <span class="built_in">scanf</span>(<span class="string">"%lld"</span>,&amp;st.a[i][<span class="number">0</span>]);</div><div class="line">            st.a[++n][<span class="number">0</span>]=<span class="number">233</span>;st.a[++n][<span class="number">0</span>]=<span class="number">1</span>;</div><div class="line"></div><div class="line">            Matrix op;</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;=n<span class="number">-2</span>;i++)</div><div class="line">            &#123;</div><div class="line">                <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j&lt;=i;j++) op.a[i][j]=<span class="number">1</span>;</div><div class="line">                op.a[i][n<span class="number">-1</span>]=<span class="number">1</span>;</div><div class="line">            &#125;</div><div class="line">            op.a[n<span class="number">-1</span>][n<span class="number">-1</span>]=<span class="number">10</span>;op.a[n<span class="number">-1</span>][n]=<span class="number">3</span>;<span class="comment">//233=&gt;2333</span></div><div class="line">            op.a[n][n]=<span class="number">1</span>;<span class="comment">//1=&gt;1</span></div><div class="line"></div><div class="line">            st=cheng(qpower(op,m),st);</div><div class="line">            <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>,st.a[n<span class="number">-2</span>][<span class="number">0</span>]);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    mine::main();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div></p>
<h3 id="24-poj2947-Widget-Factory"><a href="#24-poj2947-Widget-Factory" class="headerlink" title="24 poj2947 Widget Factory"></a>24 poj2947 Widget Factory</h3><p>8.18 难度1<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p><a href="e8f0.html">带模数的高斯消元</a><br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">namespace</span> mine</div><div class="line">&#123;</div><div class="line">    <span class="function"><span class="keyword">int</span> <span class="title">qpower</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> e,<span class="keyword">int</span> MOD)</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">int</span> ans=<span class="number">1</span>;</div><div class="line">        <span class="keyword">while</span>(e&gt;<span class="number">0</span>)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">if</span>(e&amp;<span class="number">1</span>) ans=ans*x%MOD;</div><div class="line">            x=x*x%MOD;e&gt;&gt;=<span class="number">1</span>;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> ans;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">int</span> <span class="title">inv</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> MOD)</span> </span>&#123;<span class="keyword">return</span> qpower(x,MOD<span class="number">-2</span>,MOD);&#125;</div><div class="line"></div><div class="line">    <span class="keyword">int</span> a[<span class="number">310</span>][<span class="number">310</span>],b[<span class="number">310</span>],MOD=<span class="number">7</span>;</div><div class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">iszero</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> m)</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=m;i++) <span class="keyword">if</span>(a[x][i]) <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">        <span class="keyword">return</span> <span class="number">1</span>;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">gauss</span><span class="params">(<span class="keyword">int</span> n,<span class="keyword">int</span> m)</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">int</span> tot=<span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=m;i++)<span class="comment">//目标</span></div><div class="line">        &#123;</div><div class="line">            <span class="keyword">int</span> nx=<span class="number">-1</span>;</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> j=tot+<span class="number">1</span>;j&lt;=n;j++) <span class="keyword">if</span>(a[j][i]) &#123;nx=j;<span class="keyword">break</span>;&#125;</div><div class="line">            <span class="keyword">if</span>(nx&lt;<span class="number">0</span>) <span class="keyword">continue</span>;</div><div class="line">            tot++;swap(b[tot],b[nx]);<span class="keyword">for</span>(<span class="keyword">int</span> k=<span class="number">1</span>;k&lt;=m;k++) swap(a[tot][k],a[nx][k]);</div><div class="line"></div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j&lt;=n;j++)</div><div class="line">            &#123;</div><div class="line">                <span class="keyword">if</span>(tot==j or !a[j][i]) <span class="keyword">continue</span>;</div><div class="line">                <span class="keyword">int</span> x=a[j][i],y=a[tot][i];</div><div class="line">                <span class="keyword">for</span>(<span class="keyword">int</span> k=<span class="number">1</span>;k&lt;=m;k++)</div><div class="line">                &#123;</div><div class="line">                    a[tot][k]=a[tot][k]*x%MOD;</div><div class="line">                    a[j][k]=a[j][k]*y-a[tot][k];</div><div class="line">                    a[j][k]=(a[j][k]%MOD+MOD)%MOD;</div><div class="line">                &#125;</div><div class="line">                b[tot]=b[tot]*x%MOD;</div><div class="line">                b[j]=b[j]*y%MOD-b[tot];</div><div class="line">                b[j]=(b[j]%MOD+MOD)%MOD;</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line"></div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++) <span class="keyword">if</span>(iszero(i,m) and b[i]!=<span class="number">0</span>) &#123;<span class="built_in">puts</span>(<span class="string">"Inconsistent data."</span>);<span class="keyword">return</span>;&#125;</div><div class="line">        <span class="keyword">if</span>(tot!=m) &#123;<span class="built_in">puts</span>(<span class="string">"Multiple solutions."</span>);<span class="keyword">return</span>;&#125;</div><div class="line"></div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=m<span class="number">-1</span>;i++) &#123;<span class="keyword">int</span> t=b[i]*inv(a[i][i],MOD)%MOD;<span class="built_in">printf</span>(<span class="string">"%d "</span>,t&gt;=<span class="number">3</span>?t:t+<span class="number">7</span>);&#125;</div><div class="line">        <span class="keyword">int</span> t=b[m]*inv(a[m][m],MOD)%MOD;<span class="built_in">printf</span>(<span class="string">"%d\n"</span>,t&gt;=<span class="number">3</span>?t:t+<span class="number">7</span>);<span class="comment">//debug 题目条件，3~9</span></div><div class="line">    &#125;</div><div class="line">    <span class="keyword">char</span> s[<span class="number">20</span>];</div><div class="line">    <span class="function"><span class="keyword">int</span> <span class="title">getday</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="built_in">scanf</span>(<span class="string">"%s"</span>,s+<span class="number">1</span>);</div><div class="line">        <span class="keyword">if</span>(s[<span class="number">1</span>]==<span class="string">'M'</span>) <span class="keyword">return</span> <span class="number">1</span>;<span class="comment">//MON</span></div><div class="line">        <span class="keyword">if</span>(s[<span class="number">3</span>]==<span class="string">'E'</span>) <span class="keyword">return</span> <span class="number">2</span>;<span class="comment">//TUE</span></div><div class="line">        <span class="keyword">if</span>(s[<span class="number">1</span>]==<span class="string">'W'</span>) <span class="keyword">return</span> <span class="number">3</span>;<span class="comment">//WED</span></div><div class="line">        <span class="keyword">if</span>(s[<span class="number">2</span>]==<span class="string">'H'</span>) <span class="keyword">return</span> <span class="number">4</span>;<span class="comment">//THU</span></div><div class="line">        <span class="keyword">if</span>(s[<span class="number">1</span>]==<span class="string">'F'</span>) <span class="keyword">return</span> <span class="number">5</span>;<span class="comment">//FRI</span></div><div class="line">        <span class="keyword">if</span>(s[<span class="number">2</span>]==<span class="string">'A'</span>) <span class="keyword">return</span> <span class="number">6</span>;<span class="comment">//SAT</span></div><div class="line">        <span class="keyword">return</span> <span class="number">7</span>;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">while</span>(<span class="number">1</span>)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">int</span> n,m;<span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&amp;n,&amp;m);</div><div class="line">            <span class="keyword">if</span>(n==<span class="number">0</span> and m==<span class="number">0</span>) <span class="keyword">break</span>;</div><div class="line"></div><div class="line">            <span class="built_in">memset</span>(a,<span class="number">0</span>,<span class="keyword">sizeof</span> a);<span class="comment">//debug</span></div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=m;i++)</div><div class="line">            &#123;</div><div class="line">                <span class="keyword">int</span> k;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;k);</div><div class="line">                <span class="keyword">int</span> t1=getday(),t2=getday();</div><div class="line">                b[i]=((t2-t1+<span class="number">1</span>)%MOD+MOD)%MOD;</div><div class="line">                <span class="keyword">while</span>(k--)</div><div class="line">                &#123;</div><div class="line">                    <span class="keyword">int</span> t;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;t);</div><div class="line">                    a[i][t]=(a[i][t]+<span class="number">1</span>)%MOD;</div><div class="line">                &#125;</div><div class="line">            &#125;</div><div class="line"></div><div class="line">            gauss(m,n);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    mine::main();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div></p>
<h3 id="25-WC2011-最大XOR和路径"><a href="#25-WC2011-最大XOR和路径" class="headerlink" title="25 WC2011 最大XOR和路径"></a>25 WC2011 最大XOR和路径</h3><p>8.18 难度2<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>这道神题的关键在于 <strong>利用异或的抵消性质</strong><br>当然交换、结合律也稍微要用到</p>
<p>对于一个路径，其实就是由链和在上面重叠的环组成<br>这样以后，你会发现，能对最终答案产生影响的就是一条链和几个环<br>因为对于重叠的部分或者为了到达环而经过的边（如果原路返回）被自己抵消了<br>因为这是一个无向图，如果有多条可选的链，而最优的不是这一条，<br>那当前这一条和它组成了一个环，所以说枚举环的时候异或一下，<br>自己就又被抵消了，变成了那条链</p>
<p>所以说，随便找一个链，找一些环放上去，让异或和最大<br>想到异或和会想到线性基<br>那怎么找环呢？dfs去找不难想到，但枚举起点复杂度过高<br>其实直接从1开始就好了，对于一个结束的环，可以把当前值异或从起点到这里的代价<br>这样又抵消掉了</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">namespace</span> mine</div><div class="line">&#123;</div><div class="line">    <span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</div><div class="line">    <span class="keyword">const</span> <span class="keyword">int</span> MAXN=<span class="number">51000</span>;</div><div class="line"></div><div class="line">    ll bin[<span class="number">61</span>];</div><div class="line">    ll bs[<span class="number">61</span>];</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">insert</span><span class="params">(ll now)</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">60</span>;i&gt;=<span class="number">0</span>;i--)</div><div class="line">            <span class="keyword">if</span>(now&amp;bin[i])</div><div class="line">            &#123;</div><div class="line">                <span class="keyword">if</span>(bs[i]&lt;<span class="number">0</span>)</div><div class="line">                &#123;</div><div class="line">                    bs[i]=now;</div><div class="line">                    <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">60</span>;j&gt;i;j--) <span class="keyword">if</span>(bs[j]&amp;bin[i]) bs[j]^=now;<span class="comment">//回代</span></div><div class="line">                    <span class="keyword">break</span>;</div><div class="line">                &#125;</div><div class="line">                <span class="keyword">else</span> now^=bs[i];</div><div class="line">            &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="keyword">struct</span> Nod</div><div class="line">    &#123;</div><div class="line">        <span class="keyword">int</span> hou;</div><div class="line">        <span class="keyword">bool</span> v;</div><div class="line">        ll sum;</div><div class="line">        Nod()</div><div class="line">        &#123;</div><div class="line">            hou=<span class="number">0</span>;</div><div class="line">            sum=<span class="number">0</span>;</div><div class="line">            v=<span class="number">0</span>;</div><div class="line">        &#125;</div><div class="line">    &#125;p[MAXN];</div><div class="line">    <span class="keyword">struct</span> Edge</div><div class="line">    &#123;</div><div class="line">        <span class="keyword">int</span> y,g;</div><div class="line">        ll c;</div><div class="line">    &#125;e[MAXN*<span class="number">4</span>];</div><div class="line">    <span class="keyword">int</span> ln=<span class="number">0</span>;</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">ins</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y,ll c)</span></div><div class="line">    </span>&#123;</div><div class="line">        ln++;</div><div class="line">        e[ln].y=y;e[ln].c=c;</div><div class="line">        e[ln].g=p[x].hou;p[x].hou=ln;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">dfs</span><span class="params">(<span class="keyword">int</span> x,ll now)</span></div><div class="line">    </span>&#123;</div><div class="line">        p[x].v=<span class="number">1</span>;p[x].sum=now;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> k=p[x].hou;k&gt;<span class="number">0</span>;k=e[k].g)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">int</span> y=e[k].y;</div><div class="line">            <span class="keyword">if</span>(p[y].v) insert(now^e[k].c^p[y].sum);</div><div class="line">            <span class="keyword">else</span> dfs(y,now^e[k].c);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function">ll <span class="title">solve</span><span class="params">(ll now)</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">60</span>;i&gt;=<span class="number">0</span>;i--) <span class="keyword">if</span>(bs[i]&gt;=<span class="number">0</span> and (now^bs[i])&gt;now) now=now^bs[i];</div><div class="line">        <span class="keyword">return</span> now;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        bin[<span class="number">0</span>]=<span class="number">1</span>;<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=<span class="number">60</span>;i++) bin[i]=bin[i<span class="number">-1</span>]&lt;&lt;<span class="number">1</span>;</div><div class="line"></div><div class="line">        <span class="keyword">int</span> n,m;<span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&amp;n,&amp;m);</div><div class="line">        <span class="keyword">while</span>(m--)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">int</span> x,y;ll c;<span class="built_in">scanf</span>(<span class="string">"%d%d%lld"</span>,&amp;x,&amp;y,&amp;c);</div><div class="line">            ins(x,y,c);ins(y,x,c);</div><div class="line">        &#125;</div><div class="line">        <span class="built_in">memset</span>(bs,<span class="number">-1</span>,<span class="keyword">sizeof</span> bs);</div><div class="line">        dfs(<span class="number">1</span>,<span class="number">0</span>);</div><div class="line">        <span class="built_in">printf</span>(<span class="string">"%lld"</span>,solve(p[n].sum));</div><div class="line">    &#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    mine::main();</div><div class="line">&#125;</div></pre></td></tr></table></figure>

</div></div></p>
<h3 id="26-CQOI2013-新Nim游戏"><a href="#26-CQOI2013-新Nim游戏" class="headerlink" title="26 CQOI2013 新Nim游戏"></a>26 CQOI2013 新Nim游戏</h3><p>8.19 难度2<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>根据博弈的基本知识，普通nim游戏在异或和=0的时候必败，否则必胜<br>那么只要能确保后手无法在第二回合把异或和变成0，那么就胜利</p>
<p>异或和基本上就和线性基有关了，主要是因为，对于异或线性基，<br>其异或空间除了0，其他的表示方法中每个元素最多用一次</p>
<p>什么情况下，能够把异或和变成0？<br>就是在某一次插入x中，发现无法插入<br>因为这意味着，里面的东西能够表示出x<br>这个时候，当前线性基 xor x=0，后手只要把后面取走就好了<br>我们的应对策略是把x取走（因为线性基内部，无法表示出0，否则违背定义）</p>
<p>那怎么求第一问，让取走的东西总数最小呢？<br>有一个精妙的贪心：按照从大到小考虑<br>这个可以用微扰证明，如果某个位置能用小也能用大，<br>我们用了大，不会让答案更差，因为线性基能否插入和其他位置无关<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">namespace</span> mine</div><div class="line">&#123;</div><div class="line">    <span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</div><div class="line"></div><div class="line">    <span class="keyword">int</span> bin[<span class="number">40</span>];</div><div class="line">    <span class="keyword">int</span> bs[<span class="number">40</span>];</div><div class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">insert</span><span class="params">(<span class="keyword">int</span> now)</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">40</span>;i&gt;=<span class="number">0</span>;i--)</div><div class="line">            <span class="keyword">if</span>(now&amp;bin[i])</div><div class="line">            &#123;</div><div class="line">                <span class="keyword">if</span>(bs[i]&lt;<span class="number">0</span>) &#123;bs[i]=now;<span class="keyword">return</span> <span class="number">1</span>;&#125;</div><div class="line">                now^=bs[i];</div><div class="line">            &#125;</div><div class="line">        <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="keyword">int</span> a[<span class="number">110</span>];</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        bin[<span class="number">0</span>]=<span class="number">1</span>;<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=<span class="number">30</span>;i++) bin[i]=bin[i<span class="number">-1</span>]&lt;&lt;<span class="number">1</span>;</div><div class="line">        <span class="built_in">memset</span>(bs,<span class="number">-1</span>,<span class="keyword">sizeof</span> bs);</div><div class="line"></div><div class="line">        <span class="keyword">int</span> n;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;n);</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++) <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;a[i]);</div><div class="line">        sort(a+<span class="number">1</span>,a+n+<span class="number">1</span>);</div><div class="line"></div><div class="line">        ll ans=<span class="number">0</span>,sum=<span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=n;i&gt;=<span class="number">1</span>;i--)</div><div class="line">        &#123;</div><div class="line">            sum+=a[i];</div><div class="line">            <span class="keyword">if</span>(!insert(a[i])) ans+=a[i];</div><div class="line">        &#125;</div><div class="line"></div><div class="line">        <span class="keyword">if</span>(ans==sum) <span class="built_in">puts</span>(<span class="string">"-1"</span>);</div><div class="line">        <span class="keyword">else</span> <span class="built_in">printf</span>(<span class="string">"%lld"</span>,ans);</div><div class="line">    &#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    mine::main();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div></p>
<h3 id="Sdoi2016-排列计数"><a href="#Sdoi2016-排列计数" class="headerlink" title="Sdoi2016 排列计数"></a>Sdoi2016 排列计数</h3><p>8.29 难度2<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>同学提醒这道题漏掉了……所以就没有编号了</p>
<p>把其中m个固定后，剩下的就是<a href="b51f.html">错排</a><br>发现题目要求的是$C_n^m \times D_{n-m}$<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//Zory-2018</span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">namespace</span> mine</div><div class="line">&#123;</div><div class="line">    <span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</div><div class="line">    <span class="meta">#<span class="meta-keyword">define</span> double long double</span></div><div class="line">    <span class="keyword">const</span> <span class="keyword">int</span> MAX_N=<span class="number">1100000</span>;</div><div class="line">    <span class="keyword">const</span> ll INF=<span class="number">0x3f3f3f3f</span>;</div><div class="line">    <span class="keyword">const</span> ll MOD=<span class="number">1e9</span>+<span class="number">7</span>;</div><div class="line"></div><div class="line">    ll fac[MAX_N];</div><div class="line">    ll D[MAX_N];</div><div class="line">    <span class="function">ll <span class="title">qpower</span><span class="params">(ll x,ll e)</span></div><div class="line">    </span>&#123;</div><div class="line">        ll ans=<span class="number">1</span>;</div><div class="line">        <span class="keyword">while</span>(e&gt;<span class="number">0</span>)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">if</span>(e&amp;<span class="number">1</span>) ans=ans*x%MOD;</div><div class="line">            x=x*x%MOD;e&gt;&gt;=<span class="number">1</span>;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> ans;</div><div class="line">    &#125;</div><div class="line">    <span class="function">ll <span class="title">inv</span><span class="params">(ll x)</span> </span>&#123;<span class="keyword">return</span> qpower(x,MOD<span class="number">-2</span>);&#125;</div><div class="line">    <span class="function">ll <span class="title">C</span><span class="params">(<span class="keyword">int</span> n,<span class="keyword">int</span> m)</span> </span>&#123;<span class="keyword">return</span> fac[n]*inv(fac[m])%MOD*inv(fac[n-m])%MOD;&#125;</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        fac[<span class="number">0</span>]=<span class="number">1</span>;<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;MAX_N;i++) fac[i]=fac[i<span class="number">-1</span>]*i%MOD;</div><div class="line">        D[<span class="number">0</span>]=<span class="number">1</span>;D[<span class="number">1</span>]=<span class="number">0</span>;D[<span class="number">2</span>]=<span class="number">1</span>;<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">3</span>;i&lt;MAX_N;i++) D[i]=(D[i<span class="number">-1</span>]+D[i<span class="number">-2</span>])%MOD*(i<span class="number">-1</span>)%MOD;</div><div class="line"></div><div class="line">        <span class="keyword">int</span> T;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;T);</div><div class="line">        <span class="keyword">while</span>(T--)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">int</span> n,m;<span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&amp;n,&amp;m);</div><div class="line">            <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>,C(n,m)*D[n-m]%MOD);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    mine::main();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div></p>
<h3 id="27-poj3904-Sky-Code"><a href="#27-poj3904-Sky-Code" class="headerlink" title="27 poj3904 Sky Code"></a>27 poj3904 Sky Code</h3><p>8.20 难度1<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>莫反裸题<br>一开始没想到F(d)那么好求……<br>复杂度$O(n \sqrt n)$<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">namespace</span> mine</div><div class="line">&#123;</div><div class="line">    <span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</div><div class="line">    <span class="function">ll <span class="title">C</span><span class="params">(ll n)</span><span class="comment">//C(4,n)</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">return</span> n*(n<span class="number">-1</span>)*(n<span class="number">-2</span>)*(n<span class="number">-3</span>)/<span class="number">24</span>;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="keyword">const</span> <span class="keyword">int</span> MAXNUM=<span class="number">11000</span>;</div><div class="line">    <span class="keyword">bool</span> v[MAXNUM];</div><div class="line">    <span class="keyword">int</span> pr=<span class="number">0</span>,prime[MAXNUM];</div><div class="line">    <span class="keyword">int</span> mu[MAXNUM];</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">pre</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        mu[<span class="number">1</span>]=<span class="number">1</span>;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">2</span>;i&lt;MAXNUM;i++)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">if</span>(!v[i]) prime[++pr]=i,mu[i]=<span class="number">-1</span>;</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j&lt;=pr and (ll)i*prime[j]&lt;MAXNUM;j++)</div><div class="line">            &#123;</div><div class="line">                v[i*prime[j]]=<span class="number">1</span>;</div><div class="line">                <span class="keyword">if</span>(i%prime[j]==<span class="number">0</span>) &#123;mu[i*prime[j]]=<span class="number">0</span>;<span class="keyword">break</span>;&#125;</div><div class="line">                mu[i*prime[j]]=-mu[i];</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="keyword">int</span> fd[MAXNUM];</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        pre();</div><div class="line"></div><div class="line">        <span class="keyword">int</span> n;</div><div class="line">        <span class="keyword">while</span>(<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;n)!=EOF)</div><div class="line">        &#123;</div><div class="line">            <span class="built_in">memset</span>(fd,<span class="number">0</span>,<span class="keyword">sizeof</span> fd);</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++)</div><div class="line">            &#123;</div><div class="line">                <span class="keyword">int</span> t;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;t);</div><div class="line">                <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j*j&lt;=t;j++)</div><div class="line">                &#123;</div><div class="line">                    <span class="keyword">if</span>(t%j!=<span class="number">0</span>) <span class="keyword">continue</span>;</div><div class="line">                    fd[j]++;</div><div class="line">                    <span class="keyword">if</span>(j*j!=t) fd[t/j]++;</div><div class="line">                &#125;</div><div class="line">            &#125;</div><div class="line">            ll ans=<span class="number">0</span>;</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=<span class="number">10000</span>;i++) ans+=mu[i]*C(fd[i]);</div><div class="line">            <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>,ans);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    mine::main();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div></p>
<h3 id="28-3B13-CF167B-bzoj4636-守卫者的挑战"><a href="#28-3B13-CF167B-bzoj4636-守卫者的挑战" class="headerlink" title="28 3B13/CF167B/bzoj4636 守卫者的挑战"></a>28 3B13/CF167B/bzoj4636 守卫者的挑战</h3><p>8.20 难度1<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>概率dp裸题<br>但是有很多细节，中文题目不是很清楚</p>
<ol>
<li>必须n个都尝试过，最后再离开</li>
<li>某一时刻可以装不下<figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">namespace</span> mine</div><div class="line">&#123;</div><div class="line">    <span class="function"><span class="keyword">int</span> <span class="title">mymin</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span> </span>&#123;<span class="keyword">return</span> x&lt;y?x:y;&#125;</div><div class="line">    <span class="keyword">double</span> f[<span class="number">210</span>][<span class="number">210</span>][<span class="number">410</span>];</div><div class="line">    <span class="comment">//f(i,vec,k)=【i次开始之前，赢了vec次，当前背包剩余空间为k-200】的概率</span></div><div class="line">    <span class="keyword">int</span> p[<span class="number">210</span>],a[<span class="number">210</span>];</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">int</span> n,L,K;<span class="built_in">scanf</span>(<span class="string">"%d%d%d"</span>,&amp;n,&amp;L,&amp;K);</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++) <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;p[i]);</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++) <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;a[i]);</div><div class="line"></div><div class="line">        <span class="keyword">if</span>(K&gt;<span class="number">200</span>) K=<span class="number">200</span>;<span class="comment">//debug</span></div><div class="line">        f[<span class="number">1</span>][<span class="number">0</span>][<span class="number">200</span>+K]=<span class="number">1</span>;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++)</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> vec=<span class="number">0</span>;vec&lt;=i<span class="number">-1</span>;vec++)</div><div class="line">                <span class="keyword">for</span>(<span class="keyword">int</span> bg=<span class="number">-200</span>;bg&lt;=<span class="number">200</span>;bg++)</div><div class="line">                &#123;</div><div class="line">                    f[i+<span class="number">1</span>][vec+<span class="number">1</span>][<span class="number">200</span>+mymin(bg+a[i],<span class="number">200</span>)]+=f[i][vec][<span class="number">200</span>+bg]*p[i]/<span class="number">100</span>;</div><div class="line">                    f[i+<span class="number">1</span>][vec][<span class="number">200</span>+bg]+=f[i][vec][<span class="number">200</span>+bg]*(<span class="number">100</span>-p[i])/<span class="number">100</span>;<span class="comment">//fail</span></div><div class="line">                &#125;</div><div class="line"></div><div class="line">        <span class="keyword">double</span> ans=<span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=L;i&lt;=n;i++)</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;j&lt;=<span class="number">200</span>;j++)</div><div class="line">                ans+=f[n+<span class="number">1</span>][i][<span class="number">200</span>+j];</div><div class="line">        <span class="built_in">printf</span>(<span class="string">"%.6lf"</span>,ans);</div><div class="line">    &#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    mine::main();</div><div class="line">&#125;</div></pre></td></tr></table></figure>
</li>
</ol>

</div></div></p>
<h3 id="29-poj2976-Dropping-tests"><a href="#29-poj2976-Dropping-tests" class="headerlink" title="29 poj2976 Dropping tests"></a>29 poj2976 Dropping tests</h3><p>8.20 难度2<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>01分数规划裸题<br>浮点二分果然练得还是太少了<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//Zory-2018</span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">namespace</span> mine</div><div class="line">&#123;</div><div class="line">    <span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</div><div class="line"></div><div class="line">    <span class="keyword">const</span> <span class="keyword">int</span> MAXN=<span class="number">1100</span>;</div><div class="line">    <span class="keyword">const</span> <span class="keyword">double</span> eps=<span class="number">1e-4</span>;</div><div class="line">    <span class="keyword">int</span> n,k;</div><div class="line">    ll a[MAXN],b[MAXN];</div><div class="line">    <span class="keyword">double</span> tmp[MAXN];</div><div class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">check</span><span class="params">(<span class="keyword">double</span> L)</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++) tmp[i]=(<span class="keyword">double</span>)a[i]-L*b[i];</div><div class="line">        sort(tmp+<span class="number">1</span>,tmp+n+<span class="number">1</span>);</div><div class="line">        <span class="keyword">double</span> sum=<span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=k+<span class="number">1</span>;i&lt;=n;i++) sum+=tmp[i];</div><div class="line">        <span class="keyword">return</span> sum&gt;=<span class="number">0</span>;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">while</span>(<span class="number">1</span>)</div><div class="line">        &#123;</div><div class="line">            <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&amp;n,&amp;k);<span class="keyword">if</span>(n==<span class="number">0</span> and k==<span class="number">0</span>) <span class="keyword">break</span>;</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++) <span class="built_in">scanf</span>(<span class="string">"%lld"</span>,&amp;a[i]),a[i]*=<span class="number">100</span>;</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++) <span class="built_in">scanf</span>(<span class="string">"%lld"</span>,&amp;b[i]);</div><div class="line"></div><div class="line">            <span class="keyword">double</span> l=<span class="number">0</span>,r=<span class="number">100</span>,ans=<span class="number">-1</span>;</div><div class="line">            <span class="keyword">while</span>(l&lt;=r+eps)<span class="comment">//l&lt;r</span></div><div class="line">            &#123;</div><div class="line">                <span class="keyword">double</span> mid=(l+r)/<span class="number">2</span>;</div><div class="line">                <span class="keyword">if</span>(check(mid)) ans=mid,l=mid+eps;</div><div class="line">                <span class="keyword">else</span> r=mid-eps;</div><div class="line">            &#125;</div><div class="line">            <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,<span class="keyword">int</span>(round(ans)));</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    mine::main();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div></p>
<h3 id="30-poj1704-Georgia-and-Bob"><a href="#30-poj1704-Georgia-and-Bob" class="headerlink" title="30 poj1704 Georgia and Bob"></a>30 poj1704 Georgia and Bob</h3><p>8.20 难度2<br><div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>这是一道传说中的阶梯博弈<br>其实就是转化为nim游戏</p>
<p>首先，把两堆石子两两捆绑（奇数堆的时候，把第1堆和边界0捆绑）<br>然后如果每组石子内部，后面的向前移，最多移动就是空格数量<br>所以如果不考虑前面那个的移动情况，就是一个nim游戏</p>
<p>所以对于先手，如果他是赢家，一定按照nim来<br>这个时候如果后手突然移动前面，那么先手把后面那个等距离向前移动，<br>就能还原出相同局面，而且一定能实现<br>而如果先手是输家，那么和上面同理无法改变结局<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//Zory-2018</span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">namespace</span> mine</div><div class="line">&#123;</div><div class="line">    <span class="keyword">int</span> sg[<span class="number">11000</span>];</div><div class="line">    <span class="keyword">bool</span> v[<span class="number">11000</span>];</div><div class="line">    <span class="keyword">bool</span> b[<span class="number">11000</span>];</div><div class="line">    <span class="function"><span class="keyword">int</span> <span class="title">SG</span><span class="params">(<span class="keyword">int</span> x)</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">if</span>(v[x]) <span class="keyword">return</span> sg[x];</div><div class="line">        v[x]=<span class="number">1</span>;</div><div class="line">        <span class="built_in">memset</span>(b,<span class="number">0</span>,<span class="keyword">sizeof</span> b);</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> y=<span class="number">0</span>;y&lt;=x<span class="number">-1</span>;y++) b[SG(y)]=<span class="number">1</span>;</div><div class="line">        <span class="keyword">int</span> now=<span class="number">0</span>;<span class="keyword">while</span>(b[now]) now++;</div><div class="line">        <span class="keyword">return</span> sg[x]=now;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">int</span> a[<span class="number">1100</span>];</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line">    </span>&#123;</div><div class="line">        <span class="keyword">int</span> T;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;T);</div><div class="line">        <span class="keyword">while</span>(T--)</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">int</span> n;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;n);</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++) <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;a[i]);</div><div class="line">            sort(a+<span class="number">1</span>,a+n+<span class="number">1</span>);</div><div class="line"></div><div class="line">            <span class="keyword">int</span> now=<span class="number">0</span>;</div><div class="line">            <span class="keyword">if</span>(n%<span class="number">2</span>==<span class="number">1</span>) &#123; <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i+=<span class="number">2</span>) now^=SG(a[i]-a[i<span class="number">-1</span>]<span class="number">-1</span>); &#125;</div><div class="line">            <span class="keyword">else</span> &#123; <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">2</span>;i&lt;=n;i+=<span class="number">2</span>) now^=SG(a[i]-a[i<span class="number">-1</span>]<span class="number">-1</span>); &#125;</div><div class="line"></div><div class="line">            <span class="keyword">if</span>(now&gt;<span class="number">0</span>) <span class="built_in">puts</span>(<span class="string">"Georgia will win"</span>);</div><div class="line">            <span class="keyword">else</span> <span class="built_in">puts</span>(<span class="string">"Bob will win"</span>);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    mine::main();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div></p>

      
    </div>

    
    
    

    
      <div class="post-tags">
        
          <a href="/tags/算法竞赛进阶指南/" rel="tag"># 算法竞赛进阶指南</a>
        
      </div>
    

    
      

  <div class="popular-posts-header">相关文章推荐</div>
  <ul class="popular-posts">
  
    <li class="popular-posts-item">
      
      
      <div class="popular-posts-title"><a href="/posts/18f8.html" rel="bookmark">【训练】算法竞赛进阶指南-5动态规划</a></div>
      
    </li>
  
    <li class="popular-posts-item">
      
      
      <div class="popular-posts-title"><a href="/posts/cfc5.html" rel="bookmark">【训练】算法竞赛进阶指南-0基本算法23题</a></div>
      
    </li>
  
    <li class="popular-posts-item">
      
      
      <div class="popular-posts-title"><a href="/posts/9d2f.html" rel="bookmark">【训练】算法竞赛进阶指南-2搜索13题</a></div>
      
    </li>
  
    <li class="popular-posts-item">
      
      
      <div class="popular-posts-title"><a href="/posts/7248.html" rel="bookmark">【训练】算法竞赛进阶指南-1基本数据结构21题</a></div>
      
    </li>
  
    <li class="popular-posts-item">
      
      
      <div class="popular-posts-title"><a href="/posts/59da.html" rel="bookmark">【训练】算法竞赛进阶指南-4数据结构 8题</a></div>
      
    </li>
  
  </ul>


    

	
		<div>
				<p id="div-border-left-purple">
			   <b>本文基于<a target="_blank" title="Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)" href="http://creativecommons.org/licenses/by-sa/4.0/deed.zh"> 知识共享署名-相同方式共享 4.0 </a>国际许可协议发布</b><br/>
				<span>
				<b>本文地址：</b><a href="/posts/82f7.html" title="【训练】算法竞赛进阶指南-3数学30题">http://zory.cf/posts/82f7.html</a><br/><b>转载请注明出处，谢谢！</b>
				</span>
				</p>
		</div>
    

    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>哪怕是一杯奶茶，也将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechatpay.png" alt="Zory 微信支付"/>
        <p>微信支付</p>
      </div>
    

    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      

      
      
        <div class="post-widgets">
        

        

        
          
          <div id="needsharebutton-postbottom">
            <span class="btn">
              <i class="fa fa-share-alt" aria-hidden="true"></i>
            </span>
          </div>
        
        </div>
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/posts/b37c.html" rel="next" title="洛谷18年7月月赛比赛总结">
                <i class="fa fa-chevron-left"></i> 洛谷18年7月月赛比赛总结
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/posts/f88a.html" rel="prev" title="【Abc104-D】We Love ABC">
                【Abc104-D】We Love ABC <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="http://ouk2t1pgs.bkt.clouddn.com/avatar.jpg"
                alt="Zory" />
            
              <p class="site-author-name" itemprop="name">Zory</p>
              <p class="site-description motion-element" itemprop="description">人生即使不能走最短路，仍是连通图</p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">354</span>
                    <span class="site-state-item-name">文章</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">4</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">133</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  <a href="https://github.com/Zory123" target="_blank" title="GitHub" rel="external nofollow"><i class="fa fa-fw fa-github"></i>GitHub</a>
                  
                </span>
              
                <span class="links-of-author-item">
                  <a href="mailto:1134430323@qq.com" target="_blank" title="E-Mail" rel="external nofollow"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  
                </span>
              
            </div>
          

          
          

          
          

          
            
          
          

<script type="text/javascript" src="https://api.lwl12.com/hitokoto/main/get?encode=js&charset=utf-8"></script>
<div id="lwlhitokoto">
    <script>
        lwlhitokoto()
    </script>
</div>

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#0x30-数学"><span class="nav-text">0x30 数学</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-poj2689-Prime-Distance"><span class="nav-text">1 poj2689 Prime Distance</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3101-阶乘分解"><span class="nav-text">2 3101 阶乘分解</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-HAOI2007-反素数"><span class="nav-text">3 HAOI2007 反素数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-CQOI2007-余数求和"><span class="nav-text">4 CQOI2007 余数求和</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-poj3090-Visible-Lattice-Points"><span class="nav-text">5 poj3090 Visible Lattice Points</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-poj3696-The-Luckiest-number"><span class="nav-text">6 poj3696 The Luckiest number</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#7-bzoj2973-1801-石头游戏"><span class="nav-text">7 bzoj2973 1801 石头游戏</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#8-JSOI2008-球形空间产生器"><span class="nav-text">8 JSOI2008 球形空间产生器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#9-poj1830-开关问题"><span class="nav-text">9 poj1830 开关问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#10-JLOI2015-装备购买"><span class="nav-text">10 JLOI2015 装备购买</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#11-Hdu3949-XOR"><span class="nav-text">11 Hdu3949 XOR</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#12-3602-Counting-Swaps"><span class="nav-text">12 3602 Counting Swaps</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#13-CF451E-Devu-and-Flowers"><span class="nav-text">13 CF451E Devu and Flowers</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#14-POI2007-ZAP-Queries"><span class="nav-text">14 POI2007 ZAP-Queries</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#15-3801-Rainbow的信号"><span class="nav-text">15 3801    Rainbow的信号</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#16-3802-绿豆蛙的归宿"><span class="nav-text">16 3802    绿豆蛙的归宿</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#17-3803-扑克牌"><span class="nav-text">17 3803    扑克牌</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#18-poj2311-Cutting-Game"><span class="nav-text">18 poj2311 Cutting Game</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#0x3B-数学练习"><span class="nav-text">0x3B 数学练习</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#19-SDOI2012-Longge的问题"><span class="nav-text">19 SDOI2012 Longge的问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#20-bzoj1477-poj1061-青蛙的约会"><span class="nav-text">20 bzoj1477 poj1061    青蛙的约会</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#21-3B04-Xiao-9-大战朱最学"><span class="nav-text">21 3B04    Xiao 9*大战朱最学</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#22-SDOI2011-计算器"><span class="nav-text">22 SDOI2011    计算器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#23-hdu5015-233-Matrix"><span class="nav-text">23 hdu5015 233 Matrix</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#24-poj2947-Widget-Factory"><span class="nav-text">24 poj2947 Widget Factory</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#25-WC2011-最大XOR和路径"><span class="nav-text">25 WC2011 最大XOR和路径</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#26-CQOI2013-新Nim游戏"><span class="nav-text">26 CQOI2013 新Nim游戏</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Sdoi2016-排列计数"><span class="nav-text">Sdoi2016 排列计数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#27-poj3904-Sky-Code"><span class="nav-text">27 poj3904 Sky Code</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#28-3B13-CF167B-bzoj4636-守卫者的挑战"><span class="nav-text">28 3B13/CF167B/bzoj4636 守卫者的挑战</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#29-poj2976-Dropping-tests"><span class="nav-text">29 poj2976 Dropping tests</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#30-poj1704-Georgia-and-Bob"><span class="nav-text">30 poj1704 Georgia and Bob</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2017 &mdash; <span itemprop="copyrightYear">2018</span>
  <span class="with-love" id="animate">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Zory</span>

  

  
</div>


  









  <div class="footer-custom">Hosted by <a target="_blank" rel="external nofollow" href="https://pages.coding.me"><b>Coding Pages</b></a></div>


        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>












  















  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=6.3.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=6.3.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=6.3.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=6.3.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=6.3.0"></script>



  



	





  





  








  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  
  
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail';
    guest = guest.split(',').filter(function (item) {
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: true,
        appId: 'zMDFLwV3WMlWrOYhJ7tOad3M-gzGzoHsz',
        appKey: 'cJDLxDoNgzq0JJUWQFmpCRdw',
        placeholder: '对这篇文章有什么看法？大胆地说出来吧',
        avatar:'wavatar',
        guest_info:guest,
        pageSize:'20' || 10,
    });
  </script>



  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('2');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("zMDFLwV3WMlWrOYhJ7tOad3M-gzGzoHsz", "cJDLxDoNgzq0JJUWQFmpCRdw");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            
            counter.save(null, {
              success: function(counter) {
                
                  var $element = $(document.getElementById(url));
                  $element.find('.leancloud-visitors-count').text(counter.get('time'));
                
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            
              var newcounter = new Counter();
              /* Set ACL */
              var acl = new AV.ACL();
              acl.setPublicReadAccess(true);
              acl.setPublicWriteAccess(true);
              newcounter.setACL(acl);
              /* End Set ACL */
              newcounter.set("title", title);
              newcounter.set("url", url);
              newcounter.set("time", 1);
              newcounter.save(null, {
                success: function(newcounter) {
                  var $element = $(document.getElementById(url));
                  $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
                },
                error: function(newcounter, error) {
                  console.log('Failed to create');
                }
              });
            
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  
  

  
  

  
    
      <script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
        processEscapes: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      },
      TeX: {equationNumbers: { autoNumber: "AMS" }}
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>
<script type="text/javascript" src="//cdn.jsdelivr.net/npm/mathjax@2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

    
  


  
  
  
  <script src="/lib/needsharebutton/needsharebutton.js"></script>

  <script>
    
      pbOptions = {};
      
          pbOptions.iconStyle = "box";
      
          pbOptions.boxForm = "vertical";
      
          pbOptions.position = "bottomCenter";
      
          pbOptions.networks = "Weibo,Wechat,Douban,QQZone,Twitter,Facebook,GooglePlus,Evernote,Vkontakte";
      
      new needShareButton('#needsharebutton-postbottom', pbOptions);
    
    
  </script>

  

  
  
  
  <script src="/lib/pangu/dist/pangu.min.js?v=3.3"></script>
  <script type="text/javascript">pangu.spacingPage();</script>


  
  <script type="text/javascript" src="/js/src/js.cookie.js?v=6.3.0"></script>
  <script type="text/javascript" src="/js/src/scroll-cookie.js?v=6.3.0"></script>


  

  

  
  <style>
    .copy-btn {
      display: inline-block;
      padding: 6px 12px;
      font-size: 13px;
      font-weight: 700;
      line-height: 20px;
      color: #333;
      white-space: nowrap;
      vertical-align: middle;
      cursor: pointer;
      background-color: #eee;
      background-image: linear-gradient(#fcfcfc, #eee);
      border: 1px solid #d5d5d5;
      border-radius: 3px;
      user-select: none;
      outline: 0;
    }

    .highlight-wrap .copy-btn {
      transition: opacity .3s ease-in-out;
      opacity: 0;
      padding: 2px 6px;
      position: absolute;
      right: 4px;
      top: 8px;
    }

    .highlight-wrap:hover .copy-btn,
    .highlight-wrap .copy-btn:focus {
      opacity: 1
    }

    .highlight-wrap {
      position: relative;
    }
  </style>
  <script>
    $('.highlight').each(function (i, e) {
      var $wrap = $('<div>').addClass('highlight-wrap')
      $(e).after($wrap)
      $wrap.append($('<button>').addClass('copy-btn').append('Copy').on('click', function (e) {
        var code = $(this).parent().find('.code').find('.line').map(function (i, e) {
          return $(e).text()
        }).toArray().join('\n')
        var ta = document.createElement('textarea')
        document.body.appendChild(ta)
        ta.style.position = 'absolute'
        ta.style.top = '0px'
        ta.style.left = '0px'
        ta.value = code
        ta.select()
        ta.focus()
        var result = document.execCommand('copy')
        document.body.removeChild(ta)
        
          if(result)$(this).text('Copied')
          else $(this).text('Copy failed')
        
        $(this).blur()
      })).on('mouseleave', function (e) {
        var $b = $(this).find('.copy-btn')
        setTimeout(function () {
          $b.text('Copy')
        }, 300)
      }).append(e)
    })
  </script>


</body>
</html>
